How to create multiple datasets in SAS using loops

1k views Asked by At
proc iml;
use rdata3;
read all var _all_ into pp;
close rdata3;
do i = 1 to 1050;
    perms = allperm(pp[i, ]);
    create pp&i from perms[colname= {"Best" "NA1" "NA2" "Worst"}];
    append from perms;
    close pp&i;
end;

I will like to create multiple datasets in SAS using the above code through a do loop. However, i cant seem to change the name of each dataset using the &i indicator. Can anyone help me change my code to allow me to create multiple datasets? Or are there any other alternatives on how to create multiple datasets from matrix through loops? Thanks in advance.

1

There are 1 answers

3
data _null_ On

You don't want to use macro variables you want to use the features of IML. However you will be creating an awful lot of data sets.

data rdata3;
   x = 1;
   y = 2;
   a = 4;
   b = 5;
   output;
   output;
   run;
proc iml;
   use rdata3;
   read all var _all_ into pp;
   close rdata3;
   do i = 1 to nrow(pp);
      outname = cats('pp',putn(i,'z5.'));
      perms = allperm(pp[i, ]);
      create (outname) from perms[colname= {"Best" "NA1" "NA2" "Worst"}];
      append from perms;
      close (outname);
      end;
   quit;

You can add an ID variable to PERMS and append all versions of PERMS into one data set. I'm not sure I used the best IML technique, I know just enough IML to be dangerous.

proc iml;
   use rdata3;
   read all var _all_ into pp;
   close rdata3;
   perms = j(1,5,0);
   create PP_out from perms[colname= {'ID' "Best" "NA1" "NA2" "Worst"}];
   do i = 1 to nrow(pp);
      perms = allperm(pp[i, ]);
      perms = j(nrow(perms),1,i)||perms;
      append from perms;
      end;
   close PP_out;
   quit;