Yes, I did not mean to mark that as accepted. Mouse malfunction...
Your code IS resetting pair=1 but it deleted a record for some reason. Original dataset has 49 records, output dataset now has 48. Your code deletes the record in red... It was the last record for that ID and did not have a PAIR...
ID | Start_Date | Start_Time | End_Time | Value |
2003 | 1-Jul-13 | 6:00:00 | 21:59:00 | DAY |
2003 | 1-Jul-13 | 22:00:00 | 5:59:00 | NIGHT |
2003 | 2-Jul-13 | 22:00:00 | 5:59:00 | NIGHT |
2003 | 3-Jul-13 | 6:00:00 | 21:59:00 | DAY |
2003 | 3-Jul-13 | 22:00:00 | 5:59:00 | NIGHT |
2003 | 4-Jul-13 | 6:00:00 | 21:59:00 | DAY |
2003 | 4-Jul-13 | 22:00:00 | 5:59:00 | NIGHT |
2003 | 5-Jul-13 | 6:00:00 | 21:59:00 | DAY |
Indeed! I missed the case where it was the last.id and started with an otherwise potentially valid record. This code should correct for that:
data want (drop=_:); set have; by id; length action $6; retain _next_keep _last_kept_date _pair; if first.id then _pair=0; if not last.id then do; _next=_n_+1; set have (keep=start_date value rename=(start_date=_next_start value=_next_value)) point=_next; if _next_keep='KEEP' then do; action='KEEP'; _last_kept_date=start_date; pair=_pair; output; _next_keep='DELETE'; end; else if start_date eq _next_start and value eq 'DAY' and _next_value eq 'NIGHT' then do; if 0<=(start_date-_last_kept_date)<=1 then _pair+1; else _pair=1; pair=_pair; action='KEEP'; output; _next_keep='KEEP'; end; else do; action='DELETE'; output; _next_keep='DELETE'; end; end; else do; if _next_keep eq 'KEEP' then do; action='KEEP'; pair=_pair; output; _next_keep='DELETE'; end; else do; action='DELETE'; output; _next_keep='DELETE'; end; end; run;
Art, CEO, AnalystFinder.com
Thanks for hanging in there!!! I realize what I was asking was not exactly clear at first. Will be more concise the next time. Thanks again!!!!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.