@erin3 wrote:
Previous in jury was just another variable of interest in the work flow and I wasn't sure if it was useful in building the count variable.
You're right though! The injury count sequence for id2 should be 0, 1, 2. My tired brain wrote the example incorrectly.
Can the problem still be solved? 😕
Yes, most easily through use of a "summing statement":
data want;
set have;
by id;
if first.id then injury_count=0;
injury_count+injury;
run;
The statement:
injury_count+injury;
is a "summing statement". Unlike the statement "injury_count=injury_count+injury", it primarily does two things
Adds the value of injury to the rolling total of injury_count and stores the result in injury_count. It will treat a starting value of injury_count of missing (".") as a zero if the addend injury is not also missing.
Tells SAS to retain (i.e. carry over) the injury_count value from one observation to the next. This gets the rolling total.
The "set have; by id;" statements tell SAS to expect the incoming data to be sorted by ID. It generates the temporary dummies first.id and last.id, so that you can test whether the observation in hand is at the start or end of a given BY variable group.
... View more