Hello,
How can I get the percentages of a variable against the total? This is probably so simple but I'm not a SAS expert. Thanks.
Here's an example of data:
| Month | Variable2 | Variable3 |
| JAN | x | y |
| JAN | x | x |
| JAN | y | z |
| FEB | a | b |
| MAR | zz | av |
| MAR | d | e |
I want my report to look like:
| Month | Count | Pct |
| JAN | 3 | 50% |
| FEB | 1 | 17% |
| MAR | 2 | 33% |
Thanks.
@jffeudo86 wrote:
Using coding. Not really sure how to use proc freq. Can I get the code please?
One way to get your shown output:
Proc freq data=have; tables month / nocum; run;
You didn't share the name of your data set. Use your name where I am using Have. If the name of your variable holding month values is something other than month then use your name.
Proc freq by default reports percentages to 2 decimals. If that is unacceptable then use on of the other procedures that allows more output control. Or send the output to data set and use a format to control decimals:
Proc freq data=have noprint; tables month / out=work.summary; run; Proc print data=work.summary noobs; var month count percent; format percent f3.0; run;
Can you provide real numbers? It is not obvious how the values of variable2 and variable3 are turned into your percentages.
@jffeudo86 wrote:
Using coding. Not really sure how to use proc freq. Can I get the code please?
One way to get your shown output:
Proc freq data=have; tables month / nocum; run;
You didn't share the name of your data set. Use your name where I am using Have. If the name of your variable holding month values is something other than month then use your name.
Proc freq by default reports percentages to 2 decimals. If that is unacceptable then use on of the other procedures that allows more output control. Or send the output to data set and use a format to control decimals:
Proc freq data=have noprint; tables month / out=work.summary; run; Proc print data=work.summary noobs; var month count percent; format percent f3.0; run;
Hi @jffeudo86
data have;
input (Month Variable2 Variable3) ($);
cards;
JAN x y
JAN x x
JAN y z
FEB a b
MAR zz av
MAR d e
;
proc sql;
create table want as
select month,count(month) as count,calculated count/c as pct format=percent.
from have a,(select count(*) as c from have) b
group by month ;
quit;
data have;
input (Month Variable2 Variable3) ($);
cards;
JAN x y
JAN x x
JAN y z
FEB a b
MAR zz av
MAR d e
;
data want;
do count=1 by 1 until(last.month);
set have nobs=nobs;
by month notsorted;
end;
pct=count/nobs;
format pct percent.;
drop variable:; /*drop unwanted variables*/
run;
data want;
if _n_=1 then do;
dcl hash H (dataset:'have(keep=month)',multidata:'y');
h.definekey ("month") ;
h.definedone () ;
end;
do count=1 by 1 until(last.month);
set have;
by month notsorted;
end;
pct=count/h.num_items;
format pct percent.;
drop variable:; /*drop unwanted variables*/
run;
Since you only need counts, this is the most efficient way (it avoids an additional read of the dataset for getting the whole count):
data have;
input month :$3.;
datalines;
JAN
JAN
JAN
FEB
MAR
MAR
;
proc sql noprint;
select nobs into :nobs from dictionary.tables
where libname ='WORK' and memname = 'HAVE';
quit;
data want;
set have;
by month notsorted;
retain count;
format pct percent5.;
if first.month
then count = 1;
else count + 1;
if last.month;
pct = count / &nobs.;
run;
proc print data=want noobs;
run;
Result:
month count pct JAN 3 50% FEB 1 17% MAR 2 33%
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.