Statistical programming, matrix languages, and more

Use loop variable to create data set

Reply
Contributor
Posts: 21

Use loop variable to create data set

Is it possible to use loop counter to create SAS dataset? For example, in the code below I want to create SAS datasets called test_1, test_2, and test_3.

proc iml;

A = {0};

do i = 1 to 3;
A = A || i;
print A;
create test_&i from A;
append from A;
end;


quit;
SAS Super FREQ
Posts: 3,222

Re: Use loop variable to create data set

The question is: can you define the name of a SAS data set at runtime?
I think it will be difficult to use a macro to do this because the names of the data sets aren't known until runtime, whereas the macro language is a preprocessor that substitutes code before the proc is run.

Defining the name of a SAS data set at runtime is simple in SAS/IML Studio, since you can use expressions for I/O statements such as the CREATE statement:

/* The following example runs in SAS/IML Studio, but not PROC IML */
A = {0};
do i = 1 to 3;
A = A || i;
DSName = "test_" + strip(char(i));
create (DSName) from A;
append from A;
close (DSName);
end;


In PROC IML, you can to use the EXECUTE statement to define the name of the data set at run time:
/* Similar idea; runs in PROC IML */
proc iml;

start WriteMatrix(a, DSName);
CreateStmt = "create " + DSName + " from A;";
CloseStmt = "close " + DSName + ";";
call execute(CreateStmt);
append from A;
call execute(CloseStmt);
finish;

A = {0};
do i = 1 to 3;
A = A || i;
DSName = "test_" + strip(char(i));
run WriteMatrix(A, DSName);
end;
Post a Question
Discussion Stats
  • 1 reply
  • 198 views
  • 0 likes
  • 2 in conversation