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)


 

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