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]

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