BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta1
Obsidian | Level 7
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
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller
FreelanceReinh
Jade | Level 19

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;
tarheel13
Rhodochrosite | Level 12

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 759 views
  • 3 likes
  • 4 in conversation