SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 840|回复: 7
打印 上一主题 下一主题

请教读取特定格式的txt文本

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2009-7-17 10:41:19 | 只看该作者

请教读取特定格式的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

请各位高人指教,谢谢!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2009-7-17 11:40:07 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2009-7-17 14:54:53 | 只看该作者

Re: 请教读取特定格式的txt文本

多谢hopewell,能否再在前面加一个自动搜索一个文件夹的所有.txt文本并逐个添加到数据集中,谢谢
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2009-7-17 15:22: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女
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2009-7-17 23:23:57 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2009-7-18 09:33:32 | 只看该作者

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;
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2009-7-18 14:56:21 | 只看该作者

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
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2009-7-18 23:48:19 | 只看该作者

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]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-5 21:30 , Processed in 0.094671 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表