BookmarkSubscribeRSS Feed
Vijay77
Fluorite | Level 6

i have 3 obs like below i want sum of all 3 in 4th obs

 

1)  2 (40.0)
2) 1 (20.0)
3) 2 (40.0)

 

4) 8(100)

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@Vijay77 wrote:

i have 3 obs like below i want sum of all 3 in 4th obs

 

1)  2 (40.0)
2) 1 (20.0)
3) 2 (40.0)

 

4) 8(100)


How do you get an 8 in the fourth observation?

--
Paige Miller
Vijay77
Fluorite | Level 6

apologies its 7

PaigeMiller
Diamond | Level 26

@Vijay77 wrote:

apologies its 7


I don't understand how you obtain the number 7. Please explain.

--
Paige Miller
Vijay77
Fluorite | Level 6

5

gamotte
Rhodochrosite | Level 12

hello,

 

It helps to provide have and want datasets in the form of data steps so we know exactly the type

of data you are dealing with.

 

If your observations are strings in the form you give in your example, you have

first to extract numbers, for instance with scan and input functions, then you can perform the

summations :

 

data have;
    input x $10.;
    cards;
2 (40.0)
1 (20.0)
2 (40.0)
;
run;

data want;
    set have end=fend;
    drop sum1 sum2;

    sum1+input(scan(x,1), best.);
    sum2+input(scan(x,2, '()'), best.);

    output;

    if fend then do;
        x=cat(sum1,' (',put(sum2,5.1),')');
        output;
    end;
run;

 

Reeza
Super User

In a data set?

data want;
set have end = eof;

retain total;

total + varName;

output;

if eof then do;
varName = total;
output;
end;

drop total;
run;

In a report?

proc print data=have;
var varName;
sum varName;
run;

@Vijay77 wrote:

i have 3 obs like below i want sum of all 3 in 4th obs

 

1)  2 (40.0)
2) 1 (20.0)
3) 2 (40.0)

 

4) 8(100)


 

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
  • 6 replies
  • 1266 views
  • 0 likes
  • 4 in conversation