Starting with a dataset similar to the following (with unknown number of columns / rows, but one of the column's name is ds_name):
DATA foo;
INPUT ds_name $ m1 m2 m3;
DATALINES;
ds1 1 2.1 3
ds2 3 4.7 5.3
;
RUN;
How to automatically create a generic macro program that creates the following datasets?
DATA ds1;
INPUT m1 m2 m3;
DATALINES;
1 2.1 3
;
RUN;
DATA ds2;
INPUT m1 m2 m3;
DATALINES;
3 4.7 5.3
;
RUN;
Thanks!
Is the number of different values of ds_name unknown before the process starts?
Will ds_name ever have characters other than letters, digits and _ ?
Will it ever start with a character other than letter or _?
Will the length of ds_name ever exceed 32 characters?
If the answer to any of the above is Yes you are likely to have issues.
And possibly the most import question: Why do you think that you need to split the data apart in the first place?
If the data per DS fits into memory then another approach would be to use a hash table instead of generating code. The hash output method allows you to create data sets during execution time of a data step.
The general concensus is don't do this, because SAS has many ways of processing data by groups. There are some use cases where this is required but if you find yourself later on writing macro loops, this likely isn't one.
Here's a dynamic approach:
https://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/
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.