*---Adds a new variable to a data set contain the SUM of a selected numeric variable;

 

data test;

  input id pay;

  cards;

1  234

2  345

3  200

4  654

5  123

;

run;

 

*---1. Using PROC SUMMARY ;

proc summary data=test;

  var pay;

  output out=totals sum=tot_pay;

run;

 

data tot;

  set test;

  if  _n_ = 1 then set totals(drop=_type_ _freq_);

  if tot_pay >0 then perc_pay=pay/tot_pay*100;

run;

 

proc print data=tot;

  title "Data Test with Total and Percentage" ;

run;

 

*---2. Using PROC SQL ;

proc sql;

  create table tot

  as select id,

            pay,

            sum(pay) as total,

              case

                when( sum(pay)>0 ) then pay/sum(pay)

                else .

              end as pct_pay

  from test;

quit;

 

proc print data=tot;

  title "Data Test with Total and Percentage" ;

run;