BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10
data have;
infile datalines dsd dlm=",";
length gender level $10;
	input gender $ level $;
datalines;
female, one
male, one
male, one
male, two
female, one
female, one
female, two
female, two
male, two
male, two
female, one
refused, two
female, two
male, two
; 
run;

proc freq data=have;
	table gender*level/ out=count;
run;

data have; set count;
length count_percent $10;
	count_percent=strip(put(count, 32.)) || " (" || strip(put(percent, 32.2)) || ")";
drop count percent;
run;
proc sort; by gender; run;

proc transpose data=have  out=have2 (drop= _NAME_);
	by gender;
	id level;
	var count_percent;
run;

Hi, 

I'm trying to add a totals column to this output dataset:

Hello_there_0-1649650685561.png

so that it says after gender, one, two, it says total

And there would be 7 females, 6 males, and 1 refused. 

 

Is this possible in proc freq?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile datalines dsd dlm=",";
length gender level $10;
 input gender $ level $;
datalines;
female, one
male, one
male, one
male, two
female, one
female, one
female, two
female, two
male, two
male, two
female, one
refused, two
female, two
male, two
; 
run;
ods select none;
ods output CrossTabFreqs=count ;
proc freq data=have;
 table gender*level ;
run;
ods select all;

data have; set count;
length count_percent $10;
 count_percent=strip(put(Frequency, 32.)) || " (" || strip(put(percent, 32.2)) || ")";
 if missing(level) then level='Total';
 if not missing(gender);
run;
proc sort; by gender; run;

proc transpose data=have  out=have2 (drop= _NAME_);
 by gender;
 id level;
 var count_percent;
run;

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

Not with proc freq, but with proc tabulate:

proc tabulate data=have;
   class gender level;
   table gender, level*(n pctn) all;
run;

The layout differs: count and percent are in two columns  (no, you can't combine them in proc tabulate).

Looking better:

proc tabulate data=have;
   class gender level;
   table gender= ' ', level= ' ' * (n pctn) all / box= 'Gender';
   keylabel n= ' ' pctn= ' ' all= 'total';
run;
Ksharp
Super User
data have;
infile datalines dsd dlm=",";
length gender level $10;
 input gender $ level $;
datalines;
female, one
male, one
male, one
male, two
female, one
female, one
female, two
female, two
male, two
male, two
female, one
refused, two
female, two
male, two
; 
run;
ods select none;
ods output CrossTabFreqs=count ;
proc freq data=have;
 table gender*level ;
run;
ods select all;

data have; set count;
length count_percent $10;
 count_percent=strip(put(Frequency, 32.)) || " (" || strip(put(percent, 32.2)) || ")";
 if missing(level) then level='Total';
 if not missing(gender);
run;
proc sort; by gender; run;

proc transpose data=have  out=have2 (drop= _NAME_);
 by gender;
 id level;
 var count_percent;
run;

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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
  • 1653 views
  • 3 likes
  • 3 in conversation