BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bretthouston
Obsidian | Level 7

I am hoping to filter my dataset by date in reference to the date of another variable. In the example below, I would like to keep the observation if date2 is within date1 + 7 days (or if missing). [Alternatively, I'd like to remove an observation if date2 is present and not within date1 + 7 days]. 

 

Sample dataset:

 

Obs            Date1           Date2

1               10/21/88        10/23/88      

2                6/18/89         6/30/89

3                2/20/89          2/22/89

4                4/18/87           .

 

In the set above, I would like to keep obs 1, 3 & 4.

 

Thank-you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

@bretthouston wrote:

I am hoping to filter my dataset by date in reference to the date of another variable. In the example below, I would like to keep the observation if date2 is within date1 + 7 days (or if missing). [Alternatively, I'd like to remove an observation if date2 is present and not within date1 + 7 days]. 

 

Sample dataset:

 

Obs            Date1           Date2

1               10/21/88        10/23/88      

2                6/18/89         6/30/89

3                2/20/89          2/22/89

4                4/18/87           .

 

In the set above, I would like to keep obs 1, 3 & 4.

 

Thank-you in advance!


Assuming the dates are dates and not strings, you can use subsetting-if in a datastep:

if missing(date2) or (date1 <= date2 <= date1+7);

View solution in original post

1 REPLY 1
andreas_lds
Jade | Level 19

@bretthouston wrote:

I am hoping to filter my dataset by date in reference to the date of another variable. In the example below, I would like to keep the observation if date2 is within date1 + 7 days (or if missing). [Alternatively, I'd like to remove an observation if date2 is present and not within date1 + 7 days]. 

 

Sample dataset:

 

Obs            Date1           Date2

1               10/21/88        10/23/88      

2                6/18/89         6/30/89

3                2/20/89          2/22/89

4                4/18/87           .

 

In the set above, I would like to keep obs 1, 3 & 4.

 

Thank-you in advance!


Assuming the dates are dates and not strings, you can use subsetting-if in a datastep:

if missing(date2) or (date1 <= date2 <= date1+7);

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 385 views
  • 0 likes
  • 2 in conversation