Arithmetic in SAS

Reply
Frequent Contributor
Posts: 88

Arithmetic in SAS

I need to calculate an OVERALL rate or percentage for my dataset. This involves (summing variable A)/(summing variable B and C).

 

I have already created new variables:

 

sum_a

sum_bc

 

Now what I want to do is sum_a/sum_bc 

 

However, I don't want to have to create a new variable to do this. I just want to do sum_a/sum_bc. Is there any way to do this? Proc Print? I can't figure it out.

 

 

Super User
Posts: 19,767

Re: Arithmetic in SAS

Posted in reply to christinagting0

Depends a bit on where you want this value to be displayed. 

 

You can look into the calculated option for proc report

Frequent Contributor
Posts: 88

Re: Arithmetic in SAS

I don't eally care where it display, I just want to be able to see the value that it calculates.

 

I actually also realized that my summing did not work!

 

How do I sum one entire variable?

 

this is my code in my data step:

 

sum_count_nscx=sum(count_nscx);
sum_denominator=sum(denominator);
Super User
Posts: 19,767

Re: Arithmetic in SAS

Posted in reply to christinagting0

Here's one way:

 

 

proc means data=sashelp.shoes noprint;
output out=want sum(sales inventory returns)=/autolabel;
run;

title 'Summary of totals';
proc print;run;

title 'via data step';
data output;
set want;
calc = sales / (inventory + returns);
run;

proc print data=output;
format calc percent12.2;
run;

title 'via proc sql';
proc sql;
select sales / sum(inventory, returns) as Percent format=percent12.2
from want;
quit;
Trusted Advisor
Posts: 1,553

Re: Arithmetic in SAS

Posted in reply to christinagting0

Try the next code:

 

Proc sql;

   select rate as (sum_a / sum_bc)

   from ... /* your input data set */

; quit;

 

the RATE will printed to output;

Trusted Advisor
Posts: 1,553

Re: Arithmetic in SAS

I have just now seen your answer to Reeza.

 

You can do it by:

 

Proc sql;

   select rate as (sum(count_nscx) / sum(denominator))

   from ... /* your input data set */

; quit;

 

Ask a Question
Discussion stats
  • 5 replies
  • 359 views
  • 4 likes
  • 3 in conversation