Abdul Shiyas
Abdul Shiyas

Reputation: 401

How to find percentage values of a column in sas?

Friends, In SAS how can we find percentage values of a column? My data set output and its code is given below enter image description here

and the code is

data out.calculate_age;                                                                                                                 
set out.calculate_age ;                                                                                                                 
if age = "" then age = "All";                                                                                                           
if d=. then d=0;                                                                                                                        
if s=. then s=0;                                                                                                                        
if i=. then i=0;                                                                                                                        
tot = d+s+i;     /*CALCULATING TOTAL NO. PER GROUP*/                                                                                    
PERCENTAGE=round((tot/sum(tot))*100,.1); /*PERCENTAGE PER GROUP*/                                                                       
run;

I want, in 'percentage' column, the values should be 8*100/(8+24+36+36+27) , 24*100/(8+24+36+36+27) , 36*100/(8+24+36+36+27) etc... I know sum(tot) is 8/8 , 24/24 etc...(Its calculating row wise)...

So what should I do to get right percentage?

Upvotes: 0

Views: 1858

Answers (1)

Bendy
Bendy

Reputation: 3576

You could sum up the counts for an overall total and put it into a macro variable:

proc sql ;
  select sum(d)+sum(i)+sum(s)
  into :N
  from input 
;quit ;

You can then reference this in your code:

data out.calculate_age;                                                                                                                 
set out.calculate_age ;                                                                                                                 
if age = "" then age = "All";                                                                                                           
if d=. then d=0;                                                                                                                        
if s=. then s=0;                                                                                                                        
if i=. then i=0;                                                                                                                        
tot = d+s+i;     /*CALCULATING TOTAL NO. PER GROUP*/                                                                                    
PERCENTAGE=round(tot/&N.,.1); /*PERCENTAGE PER GROUP*/                                                                       
run;

Upvotes: 1

Related Questions