2

I was trying the following codes:

proc IML;
do i=1 to 20;  
[some codes to execute]  
data[i];  
end;  
QUIT;

So I am expecting to get 20 data sets after completing the do loops. Is it possible in SAS? I can do it using macro, but I do not like to use macro within PROC IML!

Thanks in advance.

overwhelmed
  • 225
  • 3
  • 12

2 Answers2

4

If you have SAS/IML 12.1, which shipped in August 2012 as part of SAS 9.3m2, then you can just enclose the name of each data set in parentheses, like this

proc iml;
names = "Data1":"Data20";
do i = 1 to ncol(names);
   x = i;
   dsname = names[i];   /* construct each name */
   create (dsname) from x;
   append from x;
   close (dsname);
end;

For a complete program and explanation, see the last example in the article "Read data sets that are specified by an array of names."

Rick
  • 1,210
  • 6
  • 11
  • Thank you so much @Rick. I had used `dsname ={}` then I get 20 data sets named as _data1, data2_ and so on. When I re-run the codes it again produce 20 data sets starting from 21, that is _data21, data22_ and so on. Is there any way I can set my preferred name? And, is there any way I can override the data sets in case of re-run the codes? – overwhelmed Mar 31 '15 at 00:39
  • DSName needs to be a scalar. I have edited the program to make it clearer. – Rick Mar 31 '15 at 17:40
  • Awesome@Rick. I appreciate it. – overwhelmed Apr 03 '15 at 08:51
3

Yes, use the CALL EXECUTE subroutine inside of a module.

proc iml;
file LOG;

output = (1:10)`;

/*This is how you create a data set from a matrix*/
create outdata from output;
append from output;
close outdata;

/*This module will create 1 data set for each variable in OUTPUT*/
start loopit;
do i=1 to 10;
    x = output[i];
    /*build the string you want to execute*/
    outStr = 'create outdata' + catt(i) + " from x; append from x; close outdata" + catt(i) + ";";
    put outStr; /*Print the string to the log*/

    /*Execute the string*/
    call execute(outStr);
end;
finish loopit;

/*Call the module*/
call loopit;

quit;
DomPazz
  • 12,415
  • 17
  • 23