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-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1473 views
  • 1 like
  • 2 in conversation