SAS中文论坛
标题:
请教读取特定格式的txt文本
[打印本页]
作者:
shiyiming
时间:
2009-7-17 10:41
标题:
请教读取特定格式的txt文本
现有文本格式如下:
&p
bm=体重表
year=2007
name=sam
weight=54
age=15
&n
name=mary
weight=50
age=18
&n
用SAS读入后生成:
表名:wt 标签:体重表
year name weight age
-----------------------------------------
2007 sam 54 15
2007 mary 50 18
请各位高人指教,谢谢!
作者:
shiyiming
时间:
2009-7-17 11:40
标题:
Re: 请教读取特定格式的txt文本
[code:3eigke1i]data wt(label='体重表');
infile 'd:\wt.txt' end=end;
input sign $;
if sign='&p' then input bm=$10. /year=$4. /name=$20. /weight=8. /age=8.;
else if end then stop;
else input name=$20. /weight=8. /age=8.;
retain year;
drop sign bm;
run;[/code:3eigke1i]
作者:
shiyiming
时间:
2009-7-17 14:54
标题:
Re: 请教读取特定格式的txt文本
多谢hopewell,能否再在前面加一个自动搜索一个文件夹的所有.txt文本并逐个添加到数据集中,谢谢
作者:
shiyiming
时间:
2009-7-17 15:22
标题:
Re: 请教读取特定格式的txt文本
to hopewell
多谢你给的解决方法,但实际做的时候遇见一个小问题,表格中文本格式中有以数字开头的:
&p
bm=体重表
year=2007
name=sam
weight=54
age=15
[color=#FF0000:18qvpqvf]00=男[/color:18qvpqvf]
&n
name=mary
weight=50
age=18
[color=#FF0000:18qvpqvf]00=女[/color:18qvpqvf]
&n
用SAS时提示00符号不可识别,被忽略。有什么办法么。
用SAS读入后生成:
表名:wt 标签:体重表
year name weight age _00
-----------------------------------------
2007 sam 54 15男
2007 mary 50 18女
作者:
shiyiming
时间:
2009-7-17 23:23
标题:
Re: 请教读取特定格式的txt文本
我记的CMD是可以直接退出的,但忘了语句怎么写了,谁知道请告诉我,谢谢了.
[code:1c0j0nn2]%let dirpath=%nrstr(d:\);
x "cd &dirpath";
x "dir :attrib *.txt /o:-d/b >txt_name.txt";
data txt_name;
infile "&dirpath.txt_name.txt" firstobs=2;
input txt_name $;
txt_name=scan(txt_name,1,'.');
run;
%macro creatds(fname);
data &fname(label='体重表');
infile "&dirpath&fname..txt" end=end;
input sign $;
if sign='&p' then input bm=$10. /year=$4. /name=$20. /weight=8. /age=8. /_00 $5.;
else if end then stop;
else input name=$20. /weight=8. /age=8. /_00 $5.;
_00=substr(_00,4);
retain year;
drop sign bm;
run;
%mend;
%macro setds(fname,index);
data finalds;
%if &index=1 %then %do;
set &fname;
%end;
%else %do;
set finalds &fname;
%end;
run;
%mend;
data _null_;
set txt_name;
call execute('%creatds('||txt_name||')');
run;
data _null_;
set txt_name;
call execute('%setds('||txt_name||','||_n_||')');
run;[/code:1c0j0nn2]
作者:
shiyiming
时间:
2009-7-18 09:33
标题:
Re: 请教读取特定格式的txt文本
前两天学习SAS时好像见过一段程序,不知道是不是你说的CMD退出
[color=#FF0000:2u6krszh]options noxwait xmin;[/color:2u6krszh]
%let dirpath=%nrstr(d:\);
x "cd &dirpath";
x "dir :attrib *.txt /o:-d/b >txt_name.txt";
data txt_name;
infile "&dirpath.txt_name.txt" firstobs=2;
input txt_name $;
txt_name=scan(txt_name,1,'.');
run;
作者:
shiyiming
时间:
2009-7-18 14:56
标题:
Re: 请教读取特定格式的txt文本
不好意思,新的问题又以新的方式出现了,有个别班级的数据较大可能是分页的关系,底部页码,导致生成的数据错误。请再给指教一下。
&p
bm=体重表
year=2007
name=sam
weight=54
age=15
00=男
&n
name=mary
weight=50
age=18
00=女
&n
[color=#FF0000:1yjw2ttu]p=1[/color:1yjw2ttu]
&p
bm=体重表
year=2007
name=sam
weight=54
age=15
00=男
&n
name=mary
weight=50
age=18
00=女
&n
作者:
shiyiming
时间:
2009-7-18 23:48
标题:
Re: 请教读取特定格式的txt文本
谢谢zhaojxw,是noxwait选项.
[code:3k0fdnre]%macro creatds(fname);
data &fname(label='体重表' drop=sign bm);
infile "&dirpath&fname..txt" end=end;
retain year;
input sign $;
if sign='&p' then do;
input bm=$10. /year=$4. /name=$20. /weight=8. /age=8. / @4 _00 $2.;
output;
end;
else if sign='&n' then do;
if end then stop;
else do;
input sign $2. @;
if sign ne 'p=' then do;
input @1 name=$20. /weight=8. /age=8. /@4 _00 $2.;
output;
end;
end;
end;
run;
%mend;
%macro setds(fname,index);
data finalds;
%if &index=1 %then %do;
set &fname;
%end;
%else %do;
set finalds &fname;
%end;
run;
%mend;
%let dirpath=%nrstr(d:\);
options noxwait xmin;
x "cd &dirpath";
x "dir :attrib *.txt /o:-d/b >txt_name.txt";
data txt_name;
infile "&dirpath.txt_name.txt" firstobs=2;
input txt_name $;
txt_name=scan(txt_name,1,'.');
run;
data _null_;
set txt_name;
call execute('%creatds('||txt_name||')');
call execute('%setds('||txt_name||','||_n_||')');
run;[/code:3k0fdnre]
欢迎光临 SAS中文论坛 (https://mysas.net/forum/)
Powered by Discuz! X3.2