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

Hi all. 

 

I have a dataset with two variables, MY_ID and MY_DATE.  The record MY_DATEs are all between the two year period of '01JUN2016'D AND '31MAY2018'D. 

 

Using Proc SQL, I need to pull the IDs that are present with MY_DATEs between '01JUN2017'D AND '31MAY2018'D, but NOT also present between '01JUN2016'D AND '31MAY2017'D.

 

Does anyone have a suggest for a good way to code this?  Any help would be greatly appreciated.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@buechler66 wrote:

Hi all. 

 

I have a dataset with two variables, MY_ID and MY_DATE.  The record MY_DATEs are all between the two year period of '01JUN2016'D AND '31MAY2018'D. 

 

Using Proc SQL, I need to pull the IDs that are present with MY_DATEs between '01JUN2017'D AND '31MAY2018'D, but NOT also present between '01JUN2016'D AND '31MAY2017'D.

 

Does anyone have a suggest for a good way to code this?  Any help would be greatly appreciated.

 

 


Should be straight forward to translate your problem statement into SQL

/* Using Proc SQL, I need to pull
 the IDs that are present with MY_DATEs between '01JUN2017'D AND '31MAY2018'D
, but NOT also present between '01JUN2016'D AND '31MAY2017'D.
*/
proc sql ;
 select distinct MY_ID from have 
 where MY_DATE between '01JUN2017'D AND '31MAY2018'D

 except 

 select distinct MY_ID from have 
 where MY_DATE between '01JUN2016'D AND '31MAY2017'D
;
quit;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

@buechler66 wrote:

Hi all. 

 

I have a dataset with two variables, MY_ID and MY_DATE.  The record MY_DATEs are all between the two year period of '01JUN2016'D AND '31MAY2018'D. 

 

Using Proc SQL, I need to pull the IDs that are present with MY_DATEs between '01JUN2017'D AND '31MAY2018'D, but NOT also present between '01JUN2016'D AND '31MAY2017'D.

 

Does anyone have a suggest for a good way to code this?  Any help would be greatly appreciated.

 

 


Should be straight forward to translate your problem statement into SQL

/* Using Proc SQL, I need to pull
 the IDs that are present with MY_DATEs between '01JUN2017'D AND '31MAY2018'D
, but NOT also present between '01JUN2016'D AND '31MAY2017'D.
*/
proc sql ;
 select distinct MY_ID from have 
 where MY_DATE between '01JUN2017'D AND '31MAY2018'D

 except 

 select distinct MY_ID from have 
 where MY_DATE between '01JUN2016'D AND '31MAY2017'D
;
quit;
buechler66
Barite | Level 11
Thanks so much. I appreciate you taking the time to help. Much appreciated.
PGStats
Opal | Level 21

I would propose:

 

proc sql ;
 select * from have as a
 where  
    MY_DATE between '01JUN2017'D AND '31MAY2018'D and
    not exists (    select * from have 
                    where   My_ID = a.MY_ID and 
                            MY_DATE between '01JUN2016'D AND '31MAY2017'D )
;
quit;
PG

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1072 views
  • 4 likes
  • 3 in conversation