SAS: Multiple Set statements to combine and summerize datasets

A. Code

  • Gather data
%let var = field1 field2 field3 field4 field5;
%let yr = 17;
%let nextyr = 18;
data data&yr.;
length group $1.; 
set Jun&yr. (keep=&var.)
jul&yr. (keep=&var.)
nov&yr. (keep=&var.)
feb&nextyr. (keep=&var.)
mar&nextyr. (keep=&var.);
where field1= "YES";
group = substr(field2, 1, 1);
count = field3;
run;
  • Summarize data
%macro sum (varname, cntvar, group, where, out);
%put &varname.;
%put &cntvar.;
%put &where.;
proc summary nway missing data =data&yr. (where= (field4=&where.));
class field1 &group.;
var &cntvar.;
output out=&out. (drop= field1 _freq_ _type_ ) sum (&cntvar.)=&varname.;
run;
%mend sum;
%sum ( var6, count, group, 'CAT1' , t1);
%sum ( var6, count, , 'CAT1' , sum1);
%sum ( var7, count, group, 'CAT2' , t2);
%sum ( var7, count, , 'CAT2' , sum2);
%sum ( var10, count, group, 'CAT1' and field5 = 'NOV' , t3);
%sum ( var10, count, , 'CAT1' and field5 = 'NOV', sum3);
%sum ( var11, count, group, 'CAT2' and field5 = 'NOV' , t4);
%sum ( var11, count, , 'CAT2' and field = 'NOV' , sum4);
  • Combine data
data data_new;
set t1 sum1 ;
set t2 sum2;
set t3 sum3;
set t4 sum4;
if group = '' then group = 'X';
run;

B. Output

T1 output
Sum1 output
combining T1, T2, T3, T4, Sum1, Sum2, Sum3, Sum4 by group alignment

SAS: Proc Summary and Statistics Output

SUM ONE VARIABLE

%MACRO summ_prg (data=, out=);
proc summary nway missing data=&data ;
class year program level1 level2;
id progname faculty category ;
var value;
output out=&out(drop=_freq_ _type_)  sum(value)=;
run;

%mend summ_prg;

SUM MULTIPLE VARIABLES

%MACRO summ_prg (data=, out=);
proc summary nway missing data=&data ;
class year program level1 level2;
id progname faculty category ;
var value1 value2 value3;
output out=&out(drop=_freq_ _type_)  sum(value1 value2)=;
run;

%mend summ_prg;

To sum all the numerica variables, use var

_numeric_

and sum=.

CONDITIONAL SUM VARIABLES

%MACRO summ_prg (data=, out=);
proc summary nway missing data=&data (WHERE = (CON_VAR = "XXX ")) ;
class year program level1 level2;
id progname faculty category ;
var value1 value2 value3;
output out=&out(drop=_freq_ _type_)  sum(value1 value2)=;
run;

%mend summ_prg;

OUTPUT DESCRIPTIVE STATISTICS

%MACRO summ_prg (data=, out=);
proc summary nway missing data=&data ;
class year program level1 level2;
id progname faculty category ;
var value;
output out=&out(drop=_freq_ _type_) n(value) = N mean(value)=mean q1(value)=q1 q3(value) =q3;
run;
%mend summ_prg;