Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate sum in column by date

Status
Not open for further replies.

gingerfish

Programmer
Jan 20, 2011
5
CH
Hi all,

I have a small problem which is holding me back and I cannot figure it out.

In a dataset I need to calculate the sum of variable "value" and put the result in a new variable "result".
The problem is, that the calculation needs to be done by id and date. Every id can have multiple dates. I want to sum up all values that occured for an id during one date.
Example: one id has 9 observations and 3 different dates. In variable "result" I want to have 3 entries, each in the last observation for every date (last.date) and it should hold the sum of "value" for each date.

So far I have this:

Code:
...
   retain result 0;
   do i = first.id to last.id;
      do j = first.date to last.date;
         result = sum(result, value);
      end;
   end;
...

That does calculates the sum, but over all observations in the dataset. It does not end at the last date for a patient and then starts again at the next date.

I hope you can help.
Thanks a lot!
Gingerfish
 
Hi all,

I've solved it with a proc means.
Code:
proc means data=data1 sum;
   var value;
   by id date;
   output out = hlp1 sum = result;
run;

Thanks anyway!
Gingerfish
 
You could also use a Proc Summary:

proc summary data = data1 nway;
class date;
var value;
output out = hlp1 (drop = _freq_ _type_) sum=;
run;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top