BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kstolzmann
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
Astounding
PROC Star

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.

ballardw
Super User

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;

kstolzmann
Obsidian | Level 7

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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