Hi Everyone,
I want to create a list of SAS tables with the same prefix. I am not sure how to do it or if it is possible, but I need to do it with a data step.
The table names will look like this: results_group1, results_group2,.....results_group50.
Thanks
@afsand wrote:
I want to create a list of SAS tables with the same prefix. I am not sure how to do it or if it is possible, but I need to do it with a data step.
Why restrict solutions to just one method? PROC SQL works as well.
data want;
set sashelp.vtable(where=(memname=:'RESULT' and libname='WORK'));
keep memname;
run;
or
proc sql;
create table want as select memname from sashelp.vtable
where memname eqt 'RESULT' and libname='WORK';
quit;
Keep everything in one table, create a group variable, and use BY group processing in whatever you do next.
@afsand wrote:
I need to have a table for each group.
That's normally sub-optimal design done by SAS users who haven't fully understood yet the power of by-group processing.
Please tell us WHY you need separate tables and why you can't just have a single table with a variable that stores the group.
@afsand wrote:
I need to have a table for each group.
No you don't. At least in 99% of cases which we see here. Explain WHY you think you need separate datasets. Tell us about the further processing you intend to do.
@afsand wrote:
Actually I want to create a list of tables not variables.
The code I gave creates a list of tables, not variables. Exactly what you asked for.
data results_group1-results_group50;
set adherants ;
some calculations in between ;
save results in the group tables;
run;
As stated by @Kurt_Bremser , this is not a good idea, keep everything in one data set, and use an indicator or group variable to determine which group it belongs to. Don't try to split the data. Use it as one big data set, you can pull out selected parts as you need them, by using a WHERE statement, or use BY processing to handle all groups.
Please explain WHY you want to create multiple datasets?
To generate code like that you need to use code generation. Either via the SAS Macro Language or by just writing the code to a text file using a data step.
For example if you wanted to generate a data statement that names 50 output datasets you could use code like this to write that to a file.
filename code temp;
data _null_;
file code lrecl=70 ;
put 'data ' @ ;
do group=1 to 50;
put 'results_group' group @;
end;
put ';' ;
run;
Now that you have that statement in a file you could include into your program using a %INCLUDE statement.
%include code / source2;
set adherants ;
/*
some calculations in between ;
save results in the group tables;
*/
run;
Note that to actually save the results to multiple dataset will also probably involve some type of code generation. Unless you want the same thing written to all 50 datasets.
Example:
5 data class0-class1 ; - 22 200 ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_. ERROR 200-322: The symbol is not recognized and will be ignored. 6 set sashelp.class; 7 select (sex='M'); 8 when (0) output class0; 9 when (1) output class1; 10 end; 11 run; NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.CLASS0 may be incomplete. When this step was stopped there were 0 observations and 5 variables. WARNING: The data set WORK.CLASS1 may be incomplete. When this step was stopped there were 0 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.02 seconds cpu time 0.01 seconds 12 data class0 class1 ; 13 set sashelp.class; 14 select (sex='M'); 15 when (0) output class0; 16 when (1) output class1; 17 end; 18 run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.CLASS0 has 9 observations and 5 variables. NOTE: The data set WORK.CLASS1 has 10 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.