Here’s a macro that I generated which collapses variables into a single variable using an array:
* ----------------------------; * Create collapsed variables. ; %macro collapse (vars= , newvar=, dsetin=, dset=, arraynm= , i=); data &dset. (keep = key &newvar.); set sasdata.&dsetin.; array &arraynm. (&i.) &vars.; do i = 1 to &i.; if &arraynm(i) = 1 then &newvar. = i; output; end; run; proc sort data=&dset. nodupkey; by key &newvar; run; * Generate N using last. to maintain the original number of observations to calculate percentages; (OS selections were not mutually exclusive.) ; data sasdata.&dset; set &dset.; by key; if last.key then N = 1; run; proc freq data=sasdata.&dset.; tables N &newvar.; run; proc print data=sasdata.&dset.; run; %mend;
Here’s an invocation of the macro:
* --------------------------------------------------; * Create collapsed OS variable for graphing purposes; %collapse(vars= nwinxp nwin2000 nmacosx nmacos9 nlinux notheros, newvar=os, dsetin=support, dset=os, arraynm=opersys, i=6);