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!!!!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.