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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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