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
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.
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.
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;
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: