0

使用 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”语句。我是否需要查看循环函数或符号输入(在制作 proc freq 之后)?谢谢你,KKK

4

2 回答 2

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;
于 2012-03-06T01:18:57.270 回答
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;
于 2012-07-01T22:52:56.843 回答