Help using Base SAS procedures

How to write a macro with a do loop for a character variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

How to write a macro with a do loop for a character variable?

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!


Accepted Solutions
Solution
‎03-28-2017 08:37 PM
PROC Star
Posts: 7,361

Re: How to write a macro with a do loop for a character variable?

[ Edited ]

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


All Replies
Solution
‎03-28-2017 08:37 PM
PROC Star
Posts: 7,361

Re: How to write a macro with a do loop for a character variable?

[ Edited ]

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

Occasional Contributor
Posts: 16

Re: How to write a macro with a do loop for a character variable?

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)?

PROC Star
Posts: 7,361

Re: How to write a macro with a do loop for a character variable?

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

 

Art, CEO, AnalystFinder.com

 

☑ This topic is SOLVED.

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

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