BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hello,
Could you, please, help me on this subject. I use the proc report to display my results and add some additionals informations I have a table that comes as follows: var var1 var2 var3   1 100 1000 10%   2 0 500 0%   3 100 2000 5%
I want to get a following table:

var var1 var2 var3   
1 100 1000 10%   
2 0 500 0%   
3 100 2000 5%

TOTAL 200 3500 (200/3500)

Thank you
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input var  var1  var2;
cards;
  1  100   1000 
  2   0    500  
  3  100   2000
 ;
 run;
 proc report data=have nowd;
 columns var var1 var2 var3;
 define var/display;
 define var1/analysis sum;
 define var2/analysis sum;
 define var3/computed format=percent8.2;
 compute var3;
  var3=var1.sum/var2.sum;
 endcomp;

 rbreak after/summarize;
 run;

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just do it in a datastep?

data want;
   set have end=last;
   retain sum_v1 sum_v2;
   if last then do;
     output;
     var="Total";
     var1=sum_v1;
     var2=sum_v2;
     var3=cats("(",put(sum_v1,best.),"/",put(sum_v2,best.),")");
    output;
  end;
  else do;
    sum_v1=sum(sum_v1,var1);
    sum_v2=sum(sum_v2,var2);
    output;
  end;
run;
mansour_ib_sas
Pyrite | Level 9

Thank you,

 

I want to calculate this value (200/3500) too; after sum var1 and sum var2

 

var var1  var2   var3   
1 100 1000 10%   
2 0 500 0%   
3 100 2000 5%

TOTAL 200 3500 0.057

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Oh, right, I didn;t get that from the post:

data want;
   set have end=last;
   retain sum_v1 sum_v2;
   if last then do;
     output;
     var="Total";
     var1=sum_v1;
     var2=sum_v2;
     var3=sum_v1/sum_v2;
    output;
  end;
  else do;
    sum_v1=sum(sum_v1,var1);
    sum_v2=sum(sum_v2,var2);
    output;
  end;
run;
Ksharp
Super User
data have;
input var  var1  var2;
cards;
  1  100   1000 
  2   0    500  
  3  100   2000
 ;
 run;
 proc report data=have nowd;
 columns var var1 var2 var3;
 define var/display;
 define var1/analysis sum;
 define var2/analysis sum;
 define var3/computed format=percent8.2;
 compute var3;
  var3=var1.sum/var2.sum;
 endcomp;

 rbreak after/summarize;
 run;
mansour_ib_sas
Pyrite | Level 9

Hello,

 

I have another question please.

I want to calculate the ratio between var1 and the final sum of var2.

I tried to do that, but it does not work. Thank you

 

 

data have;
input var  var1  var2;
cards;
  1  100   1000 
  2   0    500  
  3  100   2000
 ;
 run; 

proc report data=have nowd;
 columns var var1 var2 var3;
 define var/display;
 define var1/analysis sum ;
 define var2/analysis sum;
 define var3/computed ;

rbreak after/summarize;
 compute  var3;
  var3=var1/var2.sum;
 endcomp;
run;

I want 

data have;
input var  var1  var2 VAR3;
cards;
  1  100   1000    100/3500
  2   0    500      0/3500
  3  100   2000    100/3500
 ;
 run; 

 

 

mansour_ib_sas
Pyrite | Level 9

 

I answer for my

data have;
input var  var1  var2;
cards;
  1  500   1000 
  2   0    500  
  3  100   2000
 ;
 run;


 proc report data=have nowd out=test;
 columns var var1 var2 var3 ;
 define var/display;
 define var1/analysis sum;
 define var2/analysis sum;
 define var3/computed format=percent8.2;
 

COMPUTE BEFORE; 
total+var2.SUM;
ENDCOMP;
COMPUTE var3;
var3=var1.sum/total;
ENDCOMP;
RBREAK AFTER /SUMMARIZE ;
RUN;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3677 views
  • 1 like
  • 3 in conversation