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!
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
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
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)?
It's the same as your original code. It creates one file by merging the n datasets.
Art, CEO, AnalystFinder.com
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.
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.