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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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