DATA Step, Macro, Functions and more

append data inside macro loop (with output statement)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

append data inside macro loop (with output statement)

Hello all:

I am trying to output obs to two different datasets inside a macro loop.  Only the obs from the last iteration of the macro are saved to my two datasets.  I am using this code:

 

%macro split (combo, drug1, class1, drug2, class2);

 

data combo_meds_split1 combo_meds_split2;

set combo_meds;

 

if drugnamewithoutdose="&combo" then do;

drugnamewithoutdose="&drug1"; drugclasscode="&class1"; output combo_meds_split1;

drugnamewithoutdose="&drug2"; drugclasscode="&class2"; output combo_meds_split2;

end;

run;

%mend split;

%split (ALISKIREN/HYDROCHLOROTHIAZIDE, ALISKIREN, CV806, HYDROCHLOROTHIAZIDE, CV701)

%split (AMLODIPINE/BENAZEPRIL, AMLODIPINE, CV200, BENAZEPRIL, CV800)

...

 

Any ideas how to get all obs for all iterations of the macro to write to the two datasets?

 

Thanks so much,

Kelly

 


Accepted Solutions
Solution
‎10-28-2015 03:15 PM
Super User
Posts: 5,085

Re: append data inside macro loop (with output statement)

Since your macro replaces the two data sets each time it runs, you will need some other data sets to hold the complete versions from all iterations.  You could, for example, add this to the end of your macro definition (before %mend):

 

proc append data=combo_meds_split1 base=all_split1;

run;

proc append data=combo_meds_split2 base=all_split2;

run;

 

Note that this always adds and never replaces.  So if you run your iterations today, you will be adding to what you created when you ran the program yesterday (if you permanently save the outputs).

 

Good luck.

View solution in original post


All Replies
Solution
‎10-28-2015 03:15 PM
Super User
Posts: 5,085

Re: append data inside macro loop (with output statement)

Since your macro replaces the two data sets each time it runs, you will need some other data sets to hold the complete versions from all iterations.  You could, for example, add this to the end of your macro definition (before %mend):

 

proc append data=combo_meds_split1 base=all_split1;

run;

proc append data=combo_meds_split2 base=all_split2;

run;

 

Note that this always adds and never replaces.  So if you run your iterations today, you will be adding to what you created when you ran the program yesterday (if you permanently save the outputs).

 

Good luck.

Super User
Posts: 10,516

Re: append data inside macro loop (with output statement)

You could use the values of your parameters to name the output sets such as

 

drugnamewithoutdose="&drug1"; drugclasscode="&class1"; output combo_meds_split1&drug1.&class1;

Occasional Contributor
Posts: 19

Re: append data inside macro loop (with output statement)

Thank you!  The append inside the macro works perfectly.  I thought about making each iteration a separate dataset (inside the macro) but the append option is nice because it saves a step of having to set all of the the resulting datasets at the end.  Thanks again, all!

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 651 views
  • 2 likes
  • 3 in conversation