DATA Step, Macro, Functions and more

Data Merge Dynamically

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Data Merge Dynamically

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


Accepted Solutions
Solution
‎06-27-2016 11:28 AM
Super User
Posts: 5,081

Re: Data Merge Dynamically

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


All Replies
Super User
Super User
Posts: 7,401

Re: Data Merge Dynamically

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;

Super User
Posts: 5,256

Re: Data Merge Dynamically

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
Solution
‎06-27-2016 11:28 AM
Super User
Posts: 5,081

Re: Data Merge Dynamically

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;

Contributor
Posts: 58

Re: Data Merge Dynamically

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

 

Thank you

 

Kody_Devl

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 246 views
  • 0 likes
  • 4 in conversation