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

Dear All,

 

I have a dataset like below with 300000 observations;

 

The calculation is ;

if A>=A then sum(B)  [if observation in A column is greater than equal to A column Sum the values in B column]

Finally, TOTAL=sum(all B)

 

Input  output 
AB AB
1100 11209
1101 21008
2102 3706
2200 4501
3205 TOTAL3424
4501   

 

Could anyone please help me here.?

 

Thanks,

Chithra

1 ACCEPTED SOLUTION

Accepted Solutions
DanielLangley
Quartz | Level 8

This code is not the full solution as it misses out the final Total. There is probably also a more efficient way of doing this. However I hope the below code helps.

 

data have;
input a b;
cards;
1 100	 
1 101
2 102
2 200
3 205
4 501
;
run;

proc sort data=have
out=haveSrt;
by descending a;
run;

data want(drop=originalB);
set haveSrt(rename=(b=originalB));
by descending a;
retain b 0;
b + originalB;
if last.a;
run;

proc sort data=want;
by a;
run;

View solution in original post

9 REPLIES 9
DanielLangley
Quartz | Level 8

This code is not the full solution as it misses out the final Total. There is probably also a more efficient way of doing this. However I hope the below code helps.

 

data have;
input a b;
cards;
1 100	 
1 101
2 102
2 200
3 205
4 501
;
run;

proc sort data=have
out=haveSrt;
by descending a;
run;

data want(drop=originalB);
set haveSrt(rename=(b=originalB));
by descending a;
retain b 0;
b + originalB;
if last.a;
run;

proc sort data=want;
by a;
run;
chithra
Quartz | Level 8

Thanks a lot, this helps

chithra
Quartz | Level 8

one more thing, is it possible to do the same with respect to  one more column?

 

data have;

input a1 $9. a b;

cards;

 

IME_TOTAL 11 100

IME_PREM 11 101

IME_TOTAL 2 102

IME_ANNUI 2 200

IME_TOTAL 3 205

IME_ANNUI 4 501

;

run;

 

output looks like :

 

Input  output  
A1ABA1AB
IME_TOTAL11100IME_TOTAL11100
IME_PREM11101IME_TOTAL3407
IME_TOTAL3102IME_PREM11101
IME_ANNUI2200IME_ANNUI2701
IME_TOTAL3205IME_ANNUI4501
IME_ANNUI4501 TOTAL1810
DanielLangley
Quartz | Level 8

What are your rules with the additional Column?

I also cannot work out what sorting method you want for the Output, please could you clarify.

 

Thanks

chithra
Quartz | Level 8

if a1=ime_total then do the calculation as done before, the sam if a1=ime_prem and ime_annui.

ie; for a1=ime_total, when a>=a sum(b). like for all a1.

 

in this example for a1=ime_total we get 2 rows :

 11>=11 --> one row

 3>=3(2 rows),3>=11 sum together--> one row

 

 

hope you will get this.

DanielLangley
Quartz | Level 8

data have;
input a1 $9. a b;
cards;
IME_TOTAL 11 100
IME_PREM 11 101
IME_TOTAL 2 102
IME_ANNUI 2 200
IME_TOTAL 3 205
IME_ANNUI 4 501
;
run;

proc sort data=have
out=haveSrt;
by a1 descending a;
run;

data want(drop=originalB);
set haveSrt(rename=(b=originalB));
by a1 descending a;
retain b 0;
if first.a1 then b=0;
b + originalB;
if last.a;
run;

proc sort data=want;
by a;
run;

Hello. This seems to match your rules but not your output. (for the three column method).

Could you have a look at it and if necessary tell me exactly which rows each row of your output should be adding up.

Apologies for this.

chithra
Quartz | Level 8

But, this actually helps me.

 

Thanks

chithra
Quartz | Level 8

Thanks a lot, this helps.

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
  • 9 replies
  • 3084 views
  • 2 likes
  • 3 in conversation