SAS:宏生成数据驱动的子集数据文件

时间:2012-03-05 18:49:30

标签: sas sas-macro

使用SAS宏,我想使用数据驱动方法生成文件子集。有人可以指导我从哪里开始? 例如,我有一个如下数据集:

data student;
 var v1 $ v2 $ v3;
 datalines ;
 f j 20
 f j 20
 f j 22
 f s 18
 f s 18
 m j 19
 m j 19
 m s 20;

我希望SAS宏识别变量中的每个值并生成数据的子集,而不是对每个变量类别使用'if'语句。我是否需要查看循环函数或symput(在制作proc freq之后)?谢谢,KKK

2 个答案:

答案 0 :(得分:2)

这是一种相当直接的方式:

data student;
   input v1 $ v2 $ v3;
   cards ;
   f j 20
   f j 20
   f j 22
   f s 18
   f s 18
   m j 19
   m j 19
   m s 20
   ;
run;

%macro make_dsns;

   proc sql;
      create table dsn_list as
         select distinct
            v1,
            v2,
            v3
         from student
      ;
   run;

   data _null_;
      retain count 0;
      set dsn_list  end=last;
      count = count + 1;
      dsn_name = trim(left(v1)) || trim(left(v2)) || trim(left(v3));
      dsn_reference = "dsn" || trim(left(count));
      call symput(dsn_reference , dsn_name);
      if last then do;
         call symput("max_count" , count);
      end;
   run;

   data
      %do count = 1 %to &max_count;
         &&dsn&count
      %end;
      ;
      set student;
      drop reference;
      reference = trim(left(v1)) || trim(left(v2)) || trim(left(v3));
      %do count = 1 %to &max_count;
         if trim(left(reference)) eq trim(left("&&dsn&count")) then output &&dsn&count;
     %end;
   run;

%mend make_dsns;

%make_dsns;

答案 1 :(得分:1)

没有宏,你可以这样做:

data _null_;

declare hash group;
group = _new_ hash(ordered: 'ascending');
group.definekey('_unique_Key');
group.definedata('v1', 'v2', 'v3');
group.definedone();

do until (last.v3);
 set student;
  by v1 v2 v3 notsorted;
  _unique_key + 1;
  group.add();
end;

 group.output(dataset: cats(v1,v2,v3));
rc = group.delete();
run;