楼主: peijiamei
4805 9

[程序分享] 如何将该逗号分隔文件读入SAS? [推广有奖]

已卖:299份资源

学科带头人

75%

还不是VIP/贵宾

-

威望
3
论坛币
26157 个
通用积分
12.7133
学术水平
217 点
热心指数
343 点
信用等级
210 点
经验
75401 点
帖子
1099
精华
1
在线时间
2016 小时
注册时间
2007-11-15
最后登录
2016-3-21

楼主
peijiamei 发表于 2010-10-26 10:58:04 |AI写论文
100论坛币
文件是以逗号分隔的,但是有的地方会出现 回车 ,但是该回车实际上不是行分隔符。
求读入该数据的程序。

写了程序不能达到效果。
将missover去掉之后也不可行。。。

帮忙同学问的,本人道行浅做不出来,求助各路大神!多谢!


程序如下:

data  BIAO3                                   ;
    infile 'E:\.样本txt' delimiter  =  ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
       informat city $6. ;
       informat quxian $18. ;
       informat yingyeting $42. ;
       informat staff_id $10. ;
       informat staff_name $33. ;
       informat SERIAL_NUMBER best13. ;
       informat RECOMM_DATE $19. ;
       informat yewu $18. ;
       informat qudao $10. ;
       informat jieshou_flag $6. ;
       format city $6. ;
       format quxian $18. ;
       format yingyeting $42. ;
       format staff_id $10. ;
       format staff_name $33. ;
       format SERIAL_NUMBER best13. ;
       format RECOMM_DATE $19. ;
       format yewu $18. ;
       format qudao $10. ;
       format jieshou_flag $6. ;
    input
                city $
                quxian $
                yingyeting $
                staff_id $
                staff_name $
                SERIAL_NUMBER
                RECOMM_DATE $
                yewu $
                qudao $
                jieshou_flag $
    ;

    run;

样本数据在附件中。

最佳答案

pobel 查看完整内容

data test; infile "c:\files\test.txt" lrecl=300 dlm='",' firstobs=2; input city :$6. quxian :$18. yingyeting : $42. staff_id : $10. staff_name : $33. SERIAL_NUMBER RECOMM_DATE : $19. yewu : $18. qudao :$10. jieshou_flag ...
关键词:delimiter informat missover informa delimit 文件 SAS 分隔 逗号
我的微博:http://t.sina.com.cn/1087192374
欢迎互相加关注!

沙发
pobel 在职认证  发表于 2010-10-26 10:58:05
data test;
   infile "c:\files\test.txt" lrecl=300 dlm='",' firstobs=2;
    input
                city :$6.
                quxian :$18.
                yingyeting : $42.
                staff_id : $10.
                staff_name : $33.
                SERIAL_NUMBER  
                RECOMM_DATE : $19.
                yewu : $18.
                qudao :$10.
                jieshou_flag :$6.
    ;
run;
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
soporaeternus + 1 + 1 + 1 好强大!
peijiamei + 2 + 1 请问dlm='",'是什么意思呢?

总评分: 学术水平 + 3  热心指数 + 2  信用等级 + 1   查看全部评分

和谐拯救危机

藤椅
Bridgenc 发表于 2010-10-26 11:04:00
样本数据在附件中???

板凳
peijiamei 发表于 2010-10-26 11:11:11
2# Bridgenc

是的呀。我刚才可能还没有写完帖子,所以没有显示出来。


现在可以看到了。我下载过了,是正常的。下载之后文件名需要改一下
我的微博:http://t.sina.com.cn/1087192374
欢迎互相加关注!

报纸
Bridgenc 发表于 2010-10-26 11:26:09
3# peijiamei

Can you try something like this, take the max length for a field:

data  BIAO3                                   ;
    infile 'E:\.样本txt' delimiter  =  ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
       input city :$6. staff_id :$10. staff_name :$33. ;
           ;
    run;

地板
Bridgenc 发表于 2010-10-26 11:30:06
It looks like a csv format file.

7
peijiamei 发表于 2010-10-26 12:06:50
dlm='",'
是什么意思呢?
我的微博:http://t.sina.com.cn/1087192374
欢迎互相加关注!

8
pobel 在职认证  发表于 2010-10-26 12:26:06
就是将双引号和逗号设置为分隔符,当多个分隔符连续出现时,如 "," 也会当作一个处理。
已有 1 人评分学术水平 热心指数 收起 理由
peijiamei + 2 + 1 如果是在变量内部存在逗号的话,这样不就出错了吗

总评分: 学术水平 + 2  热心指数 + 1   查看全部评分

和谐拯救危机

9
peijiamei 发表于 2010-10-26 13:25:45
这个程序很强大。。。

不过如果有的变量里面就有 ,的话就会出错的呀。(这个 逗号 在“”内部,不是分隔符的情况)


?????????
我的微博:http://t.sina.com.cn/1087192374
欢迎互相加关注!

10
pobel 在职认证  发表于 2010-10-26 16:08:45
如果某个变量内部有逗号的话,现在我只能想到下面的方法:
data _null_;
    infile "c:\files\test.txt" lrecl=300;
        input;
    if substr(_infile_,length(_infile_),1) =',' then _infile_=substr(_infile_,1,length(_infile_)-1);
        if substr(_infile_,length(_infile_),1) ne '"'  then _infile_=strip(_infile_)||'"';
        if substr(_infile_,1,2)='",' then _infile_=substr(_infile_,3);
        file "c:\files\test1.txt";
    put _infile_;
run;


data test;
   infile "c:\files\test1.txt" lrecl=300 dlm=',' dsd firstobs=2 ;
        input @1
                city :$6.
                quxian :$18.
                yingyeting : $42.
                staff_id : $10.
                staff_name : $33.
                SERIAL_NUMBER  
                RECOMM_DATE : $19.
                yewu : $18.
                qudao :$10.
                jieshou_flag :$6.
    ;
        put qudao=   jieshou_flag=;
run;
已有 1 人评分学术水平 热心指数 收起 理由
peijiamei + 5 + 2 致敬先~

总评分: 学术水平 + 5  热心指数 + 2   查看全部评分

和谐拯救危机

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-5 17:13