7493 21

用SAS来产生子集 [推广有奖]

11
yueCynthia 发表于 2014-3-11 17:34:27
唔..我咋感觉这个东西我要是用VBA做的话会更顺手...

12
yueCynthia 发表于 2014-3-11 18:33:02
先占个座

proc sort data=data;                                                                                                                    
by data_type;                                                                                                                           
run;                                                                                                                                    
                                                                                                                                       
proc sql;                                                                                                                              
create table b as                                                                                                                       
select distinct data_type                                                                                                               
from data;                                                                                                                              
quit;                                                                                                                                   
run;                                                                                                                                    
                                                                                                                                       
data type;                                                                                                                              
set b;                                                                                                                                 
no=_n_;                                                                                                                                 
run;                                                                                                                                    
                                                                                                                                       
data data1;                                                                                                                             
merge data type;                                                                                                                        
by data_type;                                                                                                                           
run;                                                                                                                                    
                                                                                                                                       
%macro newdataset;                                                                                                                     
%do i=1 %to 39;                                                                                                                        
data new&i;                                                                                                                             
set data1;                                                                                                                              
if no=&i;                                                                                                                              
run;                                                                                                                                    
%end;                                                                                                                                   
%mend;                                                                                                                                 
                                                                                                                                       
%newdataset;

13
小宝爱波1314 发表于 2014-3-11 19:14:44
yongyitian 发表于 2014-3-11 11:10
Sorry there are some errors that were made during cleaning and Simplifying the code after testing.
...
您用的SAS是9.3的么?我的是9.2的版本,我的log里面说


109  data _null_;
110     set types;
111     length ds $8.;
112     ds=cats ('type_', compress(type));
113     call execute('data '||ds||'; set test(where =(datatype="'||type||'")); run;');
114  run;

ERROR: Unable to load/initialize the function CATS.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

14
小宝爱波1314 发表于 2014-3-11 19:17:04
yueCynthia 发表于 2014-3-11 18:33
先占个座

proc sort data=data;                                                                     ...
您这个方法适用于数据量少的情况下吧?如果数据量大的话,datatype的分类有上百种的话,这种方法可能有点困难呀。我的数据放在二楼了,不知道为什么不能重新再发给你们。麻烦您用那个数据看看。

15
小宝爱波1314 发表于 2014-3-11 19:19:08
yongyitian 发表于 2014-3-11 09:31
您好,我的原始数据集在二楼,您能用那个做一下给我看看么?我的code还是会出现一些error。而且log不太看得懂,网上也没有找到相关的说明,希望您能帮我一下,谢谢啦

16
clara2014 发表于 2014-3-12 04:50:36
假设原数据集名为test,
%macro select;
proc sql;
select count (distinct datatype) into :kind from test;
quit;
%let kind=&kind;
proc sql ;
select distinct datatype into :type1-:type&kind from test;
quit;
%do i=1 %to &kind;
data &&type&i;
set test;
where datatype="&&type&i";
run;
%end;
%mend;
%select;

17
yongyitian 发表于 2014-3-12 08:58:58
小宝爱波1314 发表于 2014-3-11 19:17
您这个方法适用于数据量少的情况下吧?如果数据量大的话,datatype的分类有上百种的话,这种方法可能有点 ...
二楼数据中 data_type 的数据太过复杂. 会带来很多问题. 感觉需要进行整理.
要分类输出话, 需要一个新的变量来表示 data_type. 如楼上 yueCynthia 用了一个新变量 n0.  

将 data_sample duplicate_document.xlsx文件转存为 *.xls 文件,用下面的 code 运行没有问题. 但有的便变量长度太长,会有些 warning.
产生的宏变量 n 可以用来代替楼上 %do loop 中的数字 39.
  1. filename sample "F:\MySAS\Temp\InputFile\data_sample duplicate_document.xls" lrecl=32767;
  2. PROC IMPORT OUT= WORK.test
  3.   DATAFILE= sample
  4.      DBMS=EXCEL REPLACE;
  5.      SHEET="Sheet1$";
  6.      GETNAMES=YES;
  7.      MIXED=NO;
  8.      SCANTEXT=YES;
  9.      USEDATE=YES;
  10.      SCANTIME=YES;
  11. RUN;

  12. proc sort data=test;
  13.     by data_type;
  14. run;

  15. proc sql;
  16.      create table types as
  17.      select distinct data_type
  18.      from test
  19.      order by data_type;
  20. quit;

  21. data types;
  22.    set types end=last;
  23.    type='type'||put(_n_, z3.);
  24.    if last then call symput('n', _n_);
  25. run;

  26. data test_merged;
  27.     merge test types;
  28.     by data_type;
  29. run;

  30. data _null_;
  31.    set types;
  32.    length ds $8.;
  33.   call execute('data '||type||'; set test_merged(where =(type="'||type||'")); run;');
  34. run;

  35. data _null_ ;
  36. declare hash h (ordered: 'a') ;
  37.    h.definekey  ('type', '_n_') ;
  38.    h.definedata ('Center', 'Patient', 'Data_type', 'Original_entry', 'corrected_entry', 'type') ;
  39.    h.definedone ( ) ;
  40.   do _n_ = 1 by 1 until ( last.type ) ;
  41.     set test_merged;
  42.     by type ;
  43.     h.add() ;
  44.   end ;
  45.    h.output (dataset: 'Out_'|| type) ;
  46. run ;
复制代码

18
yueCynthia 发表于 2014-3-12 10:15:28
小宝爱波1314 发表于 2014-3-11 19:17
您这个方法适用于数据量少的情况下吧?如果数据量大的话,datatype的分类有上百种的话,这种方法可能有点 ...
..其实我也是个新人..上百种的话也没问题..但是我不会把数据集的名字改成对应的datatype的名字..你那个数据有39个type嘛,我已经把你那个都分成单独的了..不过数据名字叫new1..new2......new39...我在研究楼上牛人的hash

19
yueCynthia 发表于 2014-3-12 11:49:43
yongyitian 发表于 2014-3-12 08:58
二楼数据中 data_type 的数据太过复杂. 会带来很多问题. 感觉需要进行整理.
要分类输出话, 需要一 ...
大大!好激动啊你还耐心地看我代码~我的太不成熟了..
今天上午在研究你的代码....自己不懂的地方还是太多了..
搜了关于call symput、call execute、hash的来看但是还是一知半解,哪本书里会有系统地讲这些的呢?

call symput后面的代码是把序号存在n这个宏变量里吧?那这个n在后面怎么调用呢?
还有call execute括号里的引号&双引号的问题~

还有hash那段..大大有没有空给解释一下
万分感谢!!!

20
yueCynthia 发表于 2014-3-12 14:20:53
唔..反正都分好了,给你打个包发上来吧..

_TD1916.rar
下载链接: https://bbs.pinggu.org/a-1505527.html

65.15 KB

39个数据集

本附件包括:

  • new39.sas7bdat
  • new1.sas7bdat
  • new2.sas7bdat
  • new3.sas7bdat
  • new4.sas7bdat
  • new5.sas7bdat
  • new6.sas7bdat
  • new7.sas7bdat
  • new8.sas7bdat
  • new9.sas7bdat
  • new10.sas7bdat
  • new11.sas7bdat
  • new12.sas7bdat
  • new13.sas7bdat
  • new14.sas7bdat
  • new15.sas7bdat
  • new16.sas7bdat
  • new17.sas7bdat
  • new18.sas7bdat
  • new19.sas7bdat
  • new20.sas7bdat
  • new21.sas7bdat
  • new22.sas7bdat
  • new23.sas7bdat
  • new24.sas7bdat
  • new25.sas7bdat
  • new26.sas7bdat
  • new27.sas7bdat
  • new28.sas7bdat
  • new29.sas7bdat
  • new30.sas7bdat
  • new31.sas7bdat
  • new32.sas7bdat
  • new33.sas7bdat
  • new34.sas7bdat
  • new35.sas7bdat
  • new36.sas7bdat
  • new37.sas7bdat
  • new38.sas7bdat

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

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