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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 906 views
  • 0 likes
  • 2 in conversation