BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tecla1
Quartz | Level 8

Good morning, I have a big table and I need to make a cumulative sum with relative percent, like this :

 

typedateamountcumulativecumulative perc.
101/02/201850050035,71%
102/02/201820070050,00%
103/02/20187001400100,00%
201/02/201830030060,00%
202/02/2018200500100,00%

 

Can you help me? Which function I have to use?

Many Thanks for your help.

 

1 ACCEPTED SOLUTION

Accepted Solutions
singhsahab
Lapis Lazuli | Level 10

 


data want1; set have; by type; retain cumulative; if first.type then cumulative=amount; else cumulative+amount; run; proc sql; create table want as select type,date,amount,cumulative,(cumulative/max_amount)*100 format=percent8.2 as per from( select type,date,amount,cumulative,max(cumulative)as max_amount from want1 group by type) order by type,cumulative ; quit;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep in future please, as such this code is untested:

proc sql;
  create table inter as
  select a.*,
         b.tot
  from   have a
  left join (select type,sum(amount) as tot from have group by type) b
  on     a.type=b.type;
quit;

data want;
  set inter;
  retain cumulative;
  by type;
  cumulative=ifn(first.type,amount,sum(cumulative,amount));
  cumulative_perc=(cumulative/tot) * 100;
run;
Kurt_Bremser
Super User

Or use a double do loop:

data want;
sum = 0;
do until(last.type);
  set have;
  by type;
  sum + amount;
end;
cumulative = 0;
do until(last.type);
  set have;
  by type;
  cumulative + amount;
  cum_percent = cumulative / sum;
  output;
end;
drop sum;
format cum_percent percent9.2;
run;
Tecla1
Quartz | Level 8

Hi, Tnks to all, now I try … I will give you a feed back.

singhsahab
Lapis Lazuli | Level 10

 


data want1; set have; by type; retain cumulative; if first.type then cumulative=amount; else cumulative+amount; run; proc sql; create table want as select type,date,amount,cumulative,(cumulative/max_amount)*100 format=percent8.2 as per from( select type,date,amount,cumulative,max(cumulative)as max_amount from want1 group by type) order by type,cumulative ; quit;
Tecla1
Quartz | Level 8

Hi ,

 

many tnks for your solution ,

have a good afternoon !!!

Tecla

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 2085 views
  • 1 like
  • 4 in conversation