BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kody_devl
Quartz | Level 8

I have this code and it works well (hard coded);

 

/* Merge Data Tables */

data all_months;

SET data_P1, data_P2 data_P3 data_P4 data_P5 data_P6;

run;

 

The data tables are built using with a do loop macro based on a user entry of 6 (six periods). 

 

I want to dynamically be able to enter 6 or 12 or 14 or 24 periods and build this data Merge code for the "SET" portion to accomodate the different period options.

 

How can I use the number of user entered periods to build the merge statement dynamically?

 

Many Thanks.

 

Kody_Devl

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Given that you follow the naming convention you indicated for your data set names ...

 

Given that you always want a consecutive set of months ...

 

there is no need to do any complicated programming.  SAS has already built the capabilities for you.  For example:

 

data want;

set data_P7 - dataP_24;

run;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I would say that creating multiple datasets in a previous step is a bad idea straight away - hence why you now have this issue.  Generally speaking there is rarely a need to split datasets, just assign a grou value and then do processing as a by group.  Simplifies coding, makes maintenance easier, and makes further coding simpler.

 

Now if all your datasets have DATA_P prefix, then you could use that:

data want;

  set data_p:;

run;

LinusH
Tourmaline | Level 20

First, you are appending, not merging.

Then, you need to build macro logic for this. Perhaps with a %DO loop to generate the data set names dynamically on the SET statement.

Data never sleeps
Astounding
PROC Star

Given that you follow the naming convention you indicated for your data set names ...

 

Given that you always want a consecutive set of months ...

 

there is no need to do any complicated programming.  SAS has already built the capabilities for you.  For example:

 

data want;

set data_P7 - dataP_24;

run;

Kody_devl
Quartz | Level 8

This works beautifully.  I just pass in &FPer and &LPer and it is now automaitc.

 

Thank you

 

Kody_Devl

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1279 views
  • 0 likes
  • 4 in conversation