0
votes

Using SAS macro, I would like to generate subset of files using data driven method. Can someone direct me where to start? For example, I have a data set like following:

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;

Instead of using 'if' statement for each variable category, I want SAS macro recognize each value in variables and make subsets of data. Do I need to look into loop function or symput (after making proc freq)? thank you, KKK

2
I edited the post...Would the above generate a true sample data set? Can you show how this sample data would be processed?Jay Corbett
i would like to generate 5 subset data consisting of variable values like 1)f,j,20 2)f,j,22 3)m,j,19 4)m,s,20, 5)f,f,s,18. Because v1 and v2 are character variables, it is hard to use loop( or can I use do group?). I was thinking whether I should use %scan to recognise character values then use loop for v3.user634455

2 Answers

2
votes

This is one fairly straight forward way:

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
votes

Without macro you can do:

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;