Generating filename from the variable from inside the loop in SAS PROC IML

354 views Asked by At

I am trying to breakdown data file into small files, with one of the variables as a part of the name for those files. To be specific, I have a bunch of Census tracts, plus other variables. I am reading them into the matrix, perform some operations and now would like to export the data out of the loop and save it as external data file, with census tract as a part of the name; this has to be done without breaking the loop or quitting IML as I am moving onto the next tract:

read i = first census tract;
append data from other matrix;
save out file as "rld_'census_tract' value";
read next census tract;
repeat;

I tried symput function but it requires using data null inside the IML which breaks the flow.

2

There are 2 answers

2
Joe On

I don't know the solution in IML (or even if there is), but I'd suggest a different solution.

Write all of your matrices out to a single dataset (either by appending them all together or by appending to a single dataset as the loop progresses, whichever is easier), and append 'census tract' as one variable in that dataset. Then use a sas datastep to write them out to separate files afterwards. If you're talking about writing out to separate sas datasets, you can either use conditional logic or you can create a macro call to do it; if you are writing external files such as CSV or text file, you can use a filename variable (filevar option on the file statement) and write it out that way.

This will be fairly efficient (in particular for the external file method) and doesn't require staying in IML.

0
Rick On

In SAS/IML 12.1 you can use the USE and CREATE statements with arguments that are evaluated at run time.

If you have not yet upgraded to SAS/IML 12.1, you can use CALL SYMPUT and the SYMGET function to fill and retrieve a macro variable. These functions work in the IML language: no need to use DATA NULL. See my article on macros and loops in IML.