So, the data that I'm analyzing is set up in a strange way. For each person's device, there can only be 255 event numbers recorded before it rolls over, but this can happen several times. An event 0 is when the device is turned on. After a 255, there is usually a number greater than 0, indicating a roll over (unless by pure chance the device was turned off after exactly 255 cycles and turned back on again). Here is what my data looks like:
Person | Event Number | Total I Want |
---|---|---|
A | 0 | 0 |
A | 75 | 75 |
A | 79 | 79 |
A | 150 | 150 |
A | 255 | 255 |
A | 4 | 259 |
A | 20 | 275 |
A | 80 | 335 |
A | 230 | 485 |
A | 255 | 510 |
A | 36 | 546 |
B | 0 | 0 |
B | 15 | 15 |
The algorithm to do this is just eluding me. I've tried retaining and lagging and all I'm getting is nowhere. I was able to code through the first roll over, but when it hit another one, my code failed. When I try to think recursively, my mind turns to mush.
Can anyone help me? Thanks in advance.
Lauren
Corrected addition mistake. Message was edited by: Lauren Parlett
The following may do what you want:
data want (drop=base last);
set have;
by person;
if first.person then base=0;
last=lag(event_number);
if not first.person and event_number lt last then base+255;
total=sum(event_number,base);
run;
Can you provide a better explanation of what goes into the total? It appears that it is simply the event number until event number is less than the previous event number and then, for some, appears like the total is the event number added onto a cummulative sum. But that doesn't appear to hold for your example. Please explain.
Up until 255, it is just the event number.
After 255, it is the event number + 255.
If 255 comes up again, it is the event number + 255 + 255.
The following may do what you want:
data want (drop=base last);
set have;
by person;
if first.person then base=0;
last=lag(event_number);
if not first.person and event_number lt last then base+255;
total=sum(event_number,base);
run;
That totally worked, thank you!!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.