BookmarkSubscribeRSS Feed
clarkchong1
Fluorite | Level 6

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!

3 REPLIES 3
ballardw
Super User

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?

 

Patrick
Opal | Level 21

@clarkchong1 

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. 

 

Reeza
Super User

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/

 

 

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!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 616 views
  • 1 like
  • 4 in conversation