BookmarkSubscribeRSS Feed
model_coder
Calcite | Level 5
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;
1 REPLY 1
Rick_SAS
SAS Super FREQ
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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 1515 views
  • 0 likes
  • 2 in conversation