Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active March 9, 2022 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/c099e294e2a8c8b5580a to your computer and use it in GitHub Desktop.
Save statgeek/c099e294e2a8c8b5580a to your computer and use it in GitHub Desktop.
SAS - One Way Summary Table
/*
Description: Creates a One-Way Freq table of variables including percent/count
Parameters:
dsetin - inputdataset
varlist - list of variables to be analyzed separated by spaces
dsetout - name of dataset to be created
Author: F.Khurshed
Date: November 2011
Modified: September 2020
--Add in display value to show values as N(%)
*/
%macro one_way_summary(dsetin, varlist, dsetout);
proc datasets nodetails nolist;
delete &dsetout;
quit;
*loop through variable list;
%let i=1;
%do %while (%scan(&varlist, &i, " ") ^=%str());
%let var=%scan(&varlist, &i, " ");
%put &i &var;
*Cross tab;
proc freq data=&dsetin noprint;
table &var/ out=temp1;
run;
*Get variable label as name;
data _null_;
set &dsetin (obs=1);
call symput('var_name', vlabel(&var.));
run;
%put &var_name;
*Add in Variable name and store the levels as a text field;
data temp2;
keep variable value count percent display;
Variable = "&var_name";
set temp1;
value=input(&var, $50.);
percent=percent/100; * I like to store these as decimals instead of numbers;
display = catt(value, "(", put(percent, percent12.1 -l), ")");
format percent percent8.1;
drop &var.;
run;
%put &var_name;
*Append datasets;
proc append data=temp2 base=&dsetout force;
run;
/*drop temp tables so theres no accidents*/
/* proc datasets nodetails nolist; */
/* delete temp1 temp2; */
/* quit; */
*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;
%one_way_summary(sashelp.class, sex age, summary1);
proc report data=summary1 nowd;
column variable value count percent display;
define variable /order 'Variable';
define value / format=$8. 'Value';
define count/'N';
define percent/'Percentage %';
define display / 'N(%)';
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment