BookmarkSubscribeRSS Feed
brokenpc1
Fluorite | Level 6

Hi there,

 

I am new to proc report procedures, and I would like to ask a question regarding calculated value, and using calculated value to generate another calculated value. 

 

For example, using the sample data below: (Fruit is spanned header)

 

                                   Fruit
account    apple     pear    peach
sales           100       200       400
cost            40          110         180
 
I'd like to create a report using SAS proc report. Specifically, I'd like to calculate the profit, and profit % for pear only. The output should look like the following: 
 
                                     Fruit
account      apple     pear    peach
sales             100       200      400
cost                40        110         180
profit              60        90         220
profit% of pear         45%
 
Both profit and profit% of pear are calculated values. Could you please teach me how to do it using proc report?
Thank you.

 

 

 

1 REPLY 1
Ksharp
Super User
/*
It is not right tool to calculate a variable,
try data step instead.
*/

data have;
input account : $20.  apple     pear    peach;
id=1;
cards;
sales           100       200       400
cost            40          110         180
;
run;

options missing=' ';
proc report data=have nowd;
columns id account ('Fruit' apple     pear    peach);
define id/order noprint;
define account/display;
define apple/analysis sum;
define pear/analysis sum;
define peach/analysis sum;

compute peach;
 if account='sales' then do;apple_s=apple.sum;pear_s=pear.sum;peach_s=peach.sum;end;
 if account='cost'  then do;apple_c=apple.sum;pear_c=pear.sum;peach_c=peach.sum;end;
endcomp;
compute after id;
 account='profit';
 apple.sum=apple_s-apple_c;
 pear.sum=pear_s-pear_c;
 peach.sum=peach_s-peach_c;
endcomp;
compute after;
 account='profit% of pear';
 apple.sum=.;
 pear.sum=(pear_s-pear_c)/pear_s ;
 peach.sum=.;
 call define('pear.sum','format','percentn8.2');
endcomp;
break after id/summarize;
rbreak after/summarize;
run;

Ksharp_0-1693308697521.png

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 174 views
  • 1 like
  • 2 in conversation