BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jffeudo86
Quartz | Level 8

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:

MonthVariable2Variable3
JANxy
JANxx
JANyz
FEBab
MARzzav
MARde

 

I want my report to look like:

MonthCountPct
JAN350%
FEB117%
MAR233%

 

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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;

 

View solution in original post

9 REPLIES 9
Reeza
Super User
Are you using the GUI or coding? If coding use PROC FREQ, if using the GUI, try a Summary or CrossTab task to generate the summary table.
jffeudo86
Quartz | Level 8
Using coding. Not really sure how to use proc freq. Can I get the code please?
PaigeMiller
Diamond | Level 26

Can you provide real numbers? It is not obvious how the values of variable2 and variable3 are turned into your percentages.

--
Paige Miller
ballardw
Super User

@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;

 

jffeudo86
Quartz | Level 8
Thank you!
novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20
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;
novinosrin
Tourmaline | Level 20
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;
Kurt_Bremser
Super User

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% 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 9 replies
  • 1622 views
  • 3 likes
  • 6 in conversation