🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lapis Lazuli | Level 10

## How would I be able to output a totals column using proc freq when counting?

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:

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
Super User

## Re: How would I be able to output a totals column using proc freq when counting?

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;
4 REPLIES 4
Jade | Level 19

## Re: How would I be able to output a totals column using proc freq when counting?

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;
Lapis Lazuli | Level 10

Thank you!
Super User

## Re: How would I be able to output a totals column using proc freq when counting?

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;
Lapis Lazuli | Level 10

## Re: How would I be able to output a totals column using proc freq when counting?

Perfect, thanks!
Discussion stats
• 4 replies
• 843 views
• 3 likes
• 3 in conversation