data have;
infile cards ;
input ID var1 var 2 var3 ;
cards;
001 1 1 0
002 0 0 1
003 0 1 0
004 1 1 0
005 0 1 0
006 1 0 0
;
I am trying to create a simple summary dataset that contains only the frequency and percentage of a few variables (all coded as 0 or 1). Output dataset:
var_name freq percent
var1 3 50
va2 4 66.7
var3 1 16.7
proc summary data=have;
var var1 var2 var3;
output out=want(drop=_:) sum=freq1-freq3 mean=percent1-percent3;
run;
The percent will be a proportion between 0 and 1, rather than an actual percent between 0 and 100. If you want to multiply it by 100, you can do that in a DATA step or apply the PERCENT format.
proc summary data=have;
var var1 var2 var3;
output out=want(drop=_:) sum=freq1-freq3 mean=percent1-percent3;
run;
The percent will be a proportion between 0 and 1, rather than an actual percent between 0 and 100. If you want to multiply it by 100, you can do that in a DATA step or apply the PERCENT format.
Hello @lillymaginta1,
With a minor variant of @PaigeMiller's solution -- using the STACKODS option of PROC MEANS and an ODS OUTPUT statement -- you can get a bit closer to your intended dataset structure:
proc means data=have stackods sum mean;
var var1--var3;
ods output summary=want(rename=(variable=var_name sum=freq mean=percent));
run;
Printed with suitable formats, dataset WANT looks almost like your sample output:
proc format;
picture mypct (round)
0-high = "009.9" (mult=1000);
run;
proc print data=want noobs;
format freq best. percent mypct.;
run;
Proc freq already gets n and % for you.
%macro freqmac(var=);
proc freq data=have noprint;
tables var&var/ out=freq&var;
run;
data freq&var;
set freq&var;
if var&var=0 then delete;
cat="var&var";
%mend;
%freqmac(var=1)
%freqmac(var=2)
%freqmac(var=3);
data stack;
set freq1-freq3;
drop var1-var3;
run;
proc print data=stack;run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: