DATA Step, Macro, Functions and more

Adding ONE new observation.

Reply
N/A
Posts: 0

Adding ONE new observation.

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?
N/A
Posts: 0

Re: Adding ONE new observation.

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.
Respected Advisor
Posts: 3,777

Re: Adding ONE new observation.

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]
Ask a Question
Discussion stats
  • 2 replies
  • 144 views
  • 0 likes
  • 2 in conversation