Howdy
I have a very basic macro I am trying use with proc summary. I can see the problem but cannot figure out a solution. Here is my code:
%macro supersum;
proc summary
data=occ_haves;
by cluster;
%do i=1 %to 12;
var base&i;
output out=occ_category sum=sbase&i;
%end;
run;
%mend supersum;
%supersum
I would like the loop to create one data set named occ_category containing 12 summation variables ranging from sbase1 to sbase12. My code is creating a separate data set per summation variable created. So, I get errors that say:
ERROR: Data set WORK.OCC_CATEGORY is already open for output
What I do not know is how or if I can create all 12 variables in the same data set.
Any help would be appreciated.
I don't think you need a macro. Does the following approximate what you want to accomplish?:
/*create some test data*/
data occ_haves;
set sashelp.class (rename=(sex=cluster));
array sbase(14);
do i=1 to 14;
sbase(i)=height;
end;
run;
proc summary
data=occ_haves nway;
class cluster;
var sbase1-sbase12;
output out=occ_category sum= /autoname;
run;
I don't think you need a macro. Does the following approximate what you want to accomplish?:
/*create some test data*/
data occ_haves;
set sashelp.class (rename=(sex=cluster));
array sbase(14);
do i=1 to 14;
sbase(i)=height;
end;
run;
proc summary
data=occ_haves nway;
class cluster;
var sbase1-sbase12;
output out=occ_category sum= /autoname;
run;
That is some handy code. I am getting used to macros and think i unnecessarily use them at times. Why did you use a 'class cluster' in the summary versus a 'by cluster'? The /autoname is very cool as well.
thanks
When you use class, as opposed to by, you don't have to presort the dataset.
oh man, I feel so slow to have not known this but also your answer is a total score!
Don't feel slow! I've likely been using SAS for a bit longer than you have!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.