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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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