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
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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