BookmarkSubscribeRSS Feed
deleted_user
Not applicable
If the table looks like this, with 2 variables; Id and Amount:

Id Amount
Q 1200
W 1500
E 1100

I would like to add one new "observation" to the table. The new "observation" should have the values:

Id = Sum
Amount = 3800

I have my reasons to have the sum as an observation in the table.

Could this be achieved with code?
2 REPLIES 2
deleted_user
Not applicable
No problem.

data add_ob;
*use the end=last to tell us when we have reached the last observation;
set original end=last;
*output every observation we have already;
output;
*and when we have output the last original;
if last then do;
*create the values;
Id='Sum';
Amount=3800;
*and output another observation;
output;
end;
run;


Job done.
data_null__
Jade | Level 19
I think the point is to calculate the sum.

[pre]
data have;
input Id:$1. Amount;
cards;
Q 1200
W 1500
E 1100
;;;;
run;
data sum;
length ID $3; * "Sum" won't fit in $1;
do while(not eof);
set have end=eof;
output;
sum + amount;
end;
id = 'Sum';
amount = sum;
output;
stop;
drop sum;
run;
proc print;
run;
[/pre]

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 1906 views
  • 0 likes
  • 2 in conversation