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;

 

data have;

input a1 $12. a b;

cards;

 

IME_PREM_ADJ 11 100

IME_PREM_ADJ 12 102

IME_PREM_NON 11 101

IME_PREM_ADJ 13 101

IME_PREM_RES 11 105

IME_PREM_RES 3 102

IME_PREM_NON 2 200

IME_ANNUITIE 3 205

IME_ANNUITIE 4 501

;

run;

 

I need to sum the b row using a1 by group, and insert a row of TOTAL in the a column for each a1 by group with the sum.

 

The output will be :

 

IME_ANNUITIE3205
IME_ANNUITIE4501
IME_ANNUITIETOTAL706
IME_PREM_ADJ11100
IME_PREM_ADJ12102
IME_PREM_ADJ13101
IME_PREM_ADJTOTAL303
IME_PREM_NON11101
IME_PREM_NON2200
IME_PREM_NONTOTAL301
IME_PREM_RES11105
IME_PREM_RES3102
IME_PREM_RESTOTAL207

 

Thanks in advance,

Chithra

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data have;
input a1 $12. a b;
cards;
IME_PREM_ADJ 11 100
IME_PREM_ADJ 12 102
IME_PREM_NON 11 101
IME_PREM_ADJ 13 101
IME_PREM_RES 11 105
IME_PREM_RES 3 102
IME_PREM_NON 2 200
IME_ANNUITIE 3 205
IME_ANNUITIE 4 501
;
run;

data want;
  set have;
  by a1 notsorted;
  retain cnt;
  cnt=ifn(first.a1,b,sum(cnt,b));
  if not last.a1 then output;
  else do;
    output;
    a1="TOTAL";
    b=cnt;
    output;
  end;
run;

View solution in original post

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

Please use the search functionality before posting a question, I can think of at least two questions from last week which were more or less the same as this.

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

chithra
Quartz | Level 8

 yes, but which doesn't fit my answer.

I need the TOTAL's in a different way.

RW9
Diamond | Level 26 RW9
Diamond | Level 26
data have;
input a1 $12. a b;
cards;
IME_PREM_ADJ 11 100
IME_PREM_ADJ 12 102
IME_PREM_NON 11 101
IME_PREM_ADJ 13 101
IME_PREM_RES 11 105
IME_PREM_RES 3 102
IME_PREM_NON 2 200
IME_ANNUITIE 3 205
IME_ANNUITIE 4 501
;
run;

data want;
  set have;
  by a1 notsorted;
  retain cnt;
  cnt=ifn(first.a1,b,sum(cnt,b));
  if not last.a1 then output;
  else do;
    output;
    a1="TOTAL";
    b=cnt;
    output;
  end;
run;
chithra
Quartz | Level 8

Thanks a lot, this works.

FreelanceReinh
Jade | Level 19

Hi @chithra,

 

Try this:

proc sort data=have;
by a1;
run;

proc format;
value totalf
.='TOTAL';
run;

data want(drop=tb);
do until(last.a1);
  set have;
  by a1;
  tb=sum(tb,b);
  output;
end;
a=.;
b=tb;
output;
format a totalf.;
run;

proc print data=want noobs;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2195 views
  • 2 likes
  • 3 in conversation