SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to output sum of a varable in the same dataset

Reply
Occasional Contributor
Posts: 17

How to output sum of a varable in the same dataset

Hi Every one,

The data A is

Date AZ
1/1/201510
1/1/201645
1/1/201741
1/1/201841
1/1/201946
1/1/202065
1/1/202143
1/1/202259

I want it to be

Date AZ
1/1/201510
1/1/201645
1/1/201741
1/1/201841
1/1/201946
1/1/202065
1/1/202143
1/1/202259
Total350
Respected Advisor
Posts: 4,173

Re: How to output sum of a varable in the same dataset

The are many ways of how to achieve this (proc print, proc report, proc tabulate, proc sql, proc summary, ....). What have you tried so far and where did you get stuck?

Super User
Posts: 10,041

Re: How to output sum of a varable in the same dataset

If date variable is character type, that would be easy. otherwise you need other skill.

data have;
input Date : $20.     AZ ;
cards;
1/1/2015     10
1/1/2016     45
1/1/2017     41
1/1/2018     41
1/1/2019     46
1/1/2020     65
1/1/2021     43
1/1/2022     59
;
run;
data want;
 set have end=last;
 sum+az;output;
 if last then do;
   date='Total';az=sum;output;
 end;
 drop sum;
run;

Xia Keshan

Super User
Posts: 5,437

Re: How to output sum of a varable in the same dataset

Please, don't store totals in the same table as the detail data.

As Patrick suggests, creating totals is the work for reports, not data manipulation steps.

Data never sleeps
Super User
Super User
Posts: 7,980

Re: How to output sum of a varable in the same dataset

I actually disagree with this.  A case in point is my deliverables, I always store a dataset almost exactly as it is shown in the output, so that this can be independently QC'd.  Reduces the amount of QC done on the output file.  Of course this wouldn't alter the source data, just the processed data going into the output files.  Of course, something simple like this you could do with proc report for instance and have that output a dataset, however as it gets more complicated...

Respected Advisor
Posts: 4,173

Re: How to output sum of a varable in the same dataset

Storing summary rows in a table is... let's call it "very uncommon" and I would assume you'd be pretty alone with such an approach.

PROC Star
Posts: 1,167

Re: How to output sum of a varable in the same dataset

I strongly side with LInus and Patrick.

I've had considerable experience with managing aggregated data stores, and this architecture will greatly increase the chances that you'll produce incorrect summarized results in the future.

However, if you're going to do this, use something like the _TYPE_ variable that PROC MEANS creates, to provide an unambiguous indication of the role that each row plays.

Tom

Super User
Super User
Posts: 7,980

Re: How to output sum of a varable in the same dataset

Possibly a difference in industries/usage there then.  My outputs are of the form:

col1          col2          treat1          treat2          total

xyz           x               n (n%)         n (n%)        n (%)

...

And some other slightly more complicated.  I am not for a moment suggesting this should be stored in a data warehouse or what not, this is purely a dataset to support an output, and whilst with some compute blocks you may be able to get the aforementioned output we need to retain a useable copy of this so an independent QC programmer can re-create the same data for a comparison.

Note, I am not changing the raw data, merely the output supporting dataset.

Super User
Posts: 5,437

Re: How to output sum of a varable in the same dataset

For temporary tables which existence is purely for report out, then it serves a purpose.

So perhaps we agree on this matter for the main situations...? Smiley Happy

Data never sleeps
Ask a Question
Discussion stats
  • 8 replies
  • 641 views
  • 2 likes
  • 6 in conversation