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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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