DATA Step, Macro, Functions and more

adding a total (for each column) to a dataset

Reply
Regular Contributor
Posts: 196

adding a total (for each column) to a dataset

I have three variables: location, a and b:
location a b
x 1 2
y 1 2

How do I add an observation to the variable location called total where a=2 (1+1) and b=4 (2+2) so that I have:
location a b
x 1 2
y 1 2
total 2 4

Essentially what I want to do is to add an observation named total to the location variable that has the sum of columns a and b.

Thank you.
Regular Contributor
Posts: 165

Re: adding a total (for each column) to a dataset

You could do something like:

data want;
set have end=eof;
c+a;
d+b;
output;
if eof then do;
location="Total";
a=c;
b=d;
output;
end;
drop c d;
run; Message was edited by: RickM
Regular Contributor
Posts: 196

Re: adding a total (for each column) to a dataset

Thank you.
Super User
Posts: 9,681

Re: adding a total (for each column) to a dataset

Hi.
Base on this situation, I think proc tabulate and proc report would be best choice.


Ksharp
Valued Guide
Posts: 2,175

Re: adding a total (for each column) to a dataset

how about a SUM statement on PROC PRINT ?
Regular Contributor
Posts: 196

Re: adding a total (for each column) to a dataset

RickM posted what I was looking for.
The goal was to incorporate the total (for each column) into the dataset.
Thank you.
Super User
Posts: 9,681

Re: adding a total (for each column) to a dataset

Hi.
Dear Peter.
Op want ' Total :......' .
I think proc print is difficult to print 'Total' ..


Ksharp
Ask a Question
Discussion stats
  • 6 replies
  • 172 views
  • 0 likes
  • 4 in conversation