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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1767 views
  • 2 likes
  • 3 in conversation