SAS中文论坛

标题: 求助:批量将外部文件导入SAS数据集 [打印本页]

作者: shiyiming    时间: 2009-3-4 23:38
标题: 求助:批量将外部文件导入SAS数据集
如下需求,请教各位大侠如何实现!

需求:

外部文本格式的文件,实例格式如下:

---------------------------txt文件------------------------------------------
AD_CODE,VALUE,COUNT
141129,非空,546
371322,非空,3378
652925,非空,1001
500110,非空,272
350881,非空,973

AD_CODE,TYPECODE,COUNT
220283,180200,5
230123,130500,1
152201,061208,21
654202,061101,22
-------------------------------------------------------------------------------

每部分用空行标记,对空行内部分需要分别生成对应的SAS数据集,其中每一部分第一行为生成SAS数据集的变量名称,最后一个变量为数值型,如上例第二部分,AD_CODE及TYPECODE为变量名,且为字符型,COUNT为数值型变量。
作者: Qiong    时间: 2009-3-10 11:03
标题: Re: 求助:批量将外部文件导入SAS数据集
每个部分的变量个数一样么?
作者: Qiong    时间: 2009-3-10 14:13
标题: Re: 求助:批量将外部文件导入SAS数据集
比较傻的方法,如果个数一样的话,先读进来,拆成小数据集,output成小txt再读回来,可以少掉处理varname的步骤。
[code:3korqero]data WORK.test ;           
    infile 'D:\temp\test.txt' delimiter=',' MISSOVER DSD lrecl=32767 firstobs=1 ;
       informat v1-v3 $25. ;
       format v1-v3  $25. ;
    input  v1-v3  $;
run;
data _null_;
   do count=1 by 1 until (v1='' or last);
      set test end=last;
   end;
   lastobs+count;
   i+1;
   /*Data break*/
   call execute('data test' || compress(i) || ';');
   call execute('set test(firstobs=' || compress(lastobs-count+1) || ' obs=' || compress(lastobs) || ');');
   call execute('if v1^="";');
    /*output*/
    call execute('data _null_;
         set  test' || compress(i) || ';
         file "D:\temp\temp.txt" delimiter="09"x DSD DROPOVER lrecl=32767;
         put v1-v3 $;');
  /*Data in*/
   call execute ('PROC IMPORT OUT=TEST'||compress(i)||'  DATAFILE= "D:\temp\temp.txt"
    DBMS=DLM REPLACE;
     DELIMITER="09"x;
     GETNAMES=YES;
     DATAROW=2;');
         run;
run;[/code:3korqero]
作者: Qiong    时间: 2009-3-10 14:38
标题: Re: 求助:批量将外部文件导入SAS数据集
用transpose也行,但是还要专门把最后一个转成数字型。
[code:q486sls2]
data _null_;
   do count=1 by 1 until (v1='' or last);
      set test end=last;
   end;
   lastobs+count;
   i+1;
   /*Data break*/
   call execute('data temp;');
   call execute('set test(firstobs=' || compress(lastobs-count+1) || ' obs=' || compress(lastobs) || ');');
   call execute('if v1^="";');  
    /*transpose*/
    call execute('proc transpose data=temp out=temp(drop=_name_); var _all_;');
    call execute('proc transpose data=temp out=temp(drop=_name_);var col2-col4;id col1;');
    call execute('data TEST'||compress(i)||'(rename=( count1=count));');
    call execute('set temp;');
    call execute('count1=count*1;drop count;');  
  run; run; run;
[/code:q486sls2]
作者: shiyiming    时间: 2009-6-30 13:16
标题: Re: 求助:批量将外部文件导入SAS数据集
每个部分变量个数是不定的。

谢谢楼上几位的回复,虽没有最终解决问题,但是也是受益匪浅。




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2