BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Guys,
I need to do the following but I don't know how.
I need to distinguish the first case from the second:

case1:

     ID          Start                  End  

   0001           -               18DEC2019
   0001    19DEC2019             -

case2:

   ID           Start                   End  

   0001           -               18DEC2019
   0001    11JAN2020             -

 

Meaning, is there a way to flag if the end is continuous with the start from the case in which there is a gap from the previous end and the next start? Continuity in terms of years is sufficient.

Thank you in advance

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If the data are sorted by ID/start, then:

 

data want (drop=next_:);
  set have (keep=id);
  by id;
  merge have
        have (firstobs=2 keep=start rename=(start=next_start);

  if first.id=1 or start-1 ^=lag(end) then backward_consecutive='N';
  else backwards_consecutive='Y';

  if last.id=1 or end+1 ^= next_start then forward_consecutive='N';
  else forward_consecutive='Y';
run;

 

This uses a self-merge with offset approach  (i.e. the firstobs=2 in one of the MERGE argument.  It also uses SET plus BY statements to generate the first.id and last.id dummies.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

If the data are sorted by ID/start, then:

 

data want (drop=next_:);
  set have (keep=id);
  by id;
  merge have
        have (firstobs=2 keep=start rename=(start=next_start);

  if first.id=1 or start-1 ^=lag(end) then backward_consecutive='N';
  else backwards_consecutive='Y';

  if last.id=1 or end+1 ^= next_start then forward_consecutive='N';
  else forward_consecutive='Y';
run;

 

This uses a self-merge with offset approach  (i.e. the firstobs=2 in one of the MERGE argument.  It also uses SET plus BY statements to generate the first.id and last.id dummies.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
NewUsrStat
Lapis Lazuli | Level 10
Thank you very much! It works perfectly!

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 740 views
  • 2 likes
  • 2 in conversation