athresh
athresh

Reputation: 563

Column total as an observation in a dataset in SAS

I need a column a total as an observation.

Input Dataset           Output Dataset
-------------           --------------
data input;             Name    Mark
input name$ mark;       a       10
datalines;              b       20
a 10                    c       30
b 20                    Total   60
c 30
;
run;

The below code which I wrote is working fine.

data output;
  set input end=eof;
  tot + mark;
  if eof then
  do;
    output;
    name = 'Total';
    mark = tot;
    output;
  end;
  else output;
run;

Please suggest if there is any better way of doing this.

Upvotes: 1

Views: 13799

Answers (2)

Joe
Joe

Reputation: 63434

PROC REPORT is a good solution for doing this. This summarizes the entire report - other options give you the ability to summarize in groups.

proc report out=outds data=input nowd;
columns name mark;
define name/group;
define mark/analysis sum;
compute after;
  name = "Total";
  line "Total" mark.sum;
endcomp;
run;

Upvotes: 2

vasja
vasja

Reputation: 4792

Your code is fine in general, however the issue might be in terms of performance. If the input table is huge, you end up rewriting full table.

I'd suggest something like this:

proc sql;
delete from input where name = 'Total';
create table total as
select 'Total' as name length=8, sum(mark) as mark
from input
;
quit;

proc append base=input data=total;
run;

Here you are reading full table but writing only a single row to existing table.

Upvotes: 1

Related Questions