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

I'm trying to figure out the best way to run this program.

 

I have multiple datasets that I'm hoping to create a macro for in order to merge.  The issue is that the number of datasets I'm merging will vary.  (i.e. all will be named test1 to testn, but the exact number that I merge at a time will differ).  I was wondering if it's possible to use a do statement in a macro for this?  I've tried the code below but it's having difficulty.

 

%MACRO merge_sort (testnum=);
%DO i=1 %TO &testnum

PROC SORT DATA= test&i;
BY ID MonthYear;
RUN;

%END;
%mend;

 

%MACRO merge_transpose (dataout= testnum=)

DATA SOURCE.&dataout
MERGE test1--&testnum;
BY ID MonthYear;
RUN;
%mend;

 

 

Does anyone have advice?  I also tried running with the I'm fairly new to macros and do-steps, so any advice is helpful.  Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Shouldn't that be:

 

MERGE test1-test&testnum;

rather than:

MERGE test1--&testnum;

i.e, only one - and the filename starting with test?

 

And, why have two macros when you can do everything with just one. e.g.:

 

%MACRO merge_transpose (dataout= testnum=);
  %DO i=1 %TO &testnum;
     PROC SORT DATA= test&i;
       BY ID MonthYear;
    RUN;
  %END;
  DATA SOURCE.&dataout;
    MERGE test1-test&testnum;
    BY ID MonthYear;
  RUN;
%mend;

 

Art, CEO, AnalystFinder.com

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Shouldn't that be:

 

MERGE test1-test&testnum;

rather than:

MERGE test1--&testnum;

i.e, only one - and the filename starting with test?

 

And, why have two macros when you can do everything with just one. e.g.:

 

%MACRO merge_transpose (dataout= testnum=);
  %DO i=1 %TO &testnum;
     PROC SORT DATA= test&i;
       BY ID MonthYear;
    RUN;
  %END;
  DATA SOURCE.&dataout;
    MERGE test1-test&testnum;
    BY ID MonthYear;
  RUN;
%mend;

 

Art, CEO, AnalystFinder.com

cyoung
Fluorite | Level 6

Thank you!! That makes sense!

 

Though question (sorry if this is a silly one), when running the do loop for the second step- will this create 1 merged dataset for all of the sorted datasets or n merged datasets (that are just test1 and testn)?

art297
Opal | Level 21

It's the same as your original code. It creates one file by merging the n datasets.

 

Art, CEO, AnalystFinder.com

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1084 views
  • 1 like
  • 2 in conversation