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

 

I want to create an exclusion criteria using the years within a variable. The data looks like:

 

DATA TEST;
    INPUT ID DATA_VALID ID_YEAR;
DATALINES;
1 01APR2012 2013
2 14DEC2014 2015
2 27AUG2010 2009
;

I want to perform the logical statement: If the DATA_VALID observation is after the ID_YEAR observation of December 31 YYYY, flag it as a 0. 

 

DATA WANT:
    SET TEST;
    IF DATA_VALID >  '31DEC+"ID_YEAR"'d THEN flag= 1;
        ELSE flag=0;
RUN;

The first two observations should be set to 0, with the last (ID=3) equal to 1. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In order to be able to work with dates, they must be read in using the proper informat to make them dates and not text.

DATA TEST;
    INPUT ID DATA_VALID date9. ID_YEAR;
DATALINES;
1 01APR2012 2013
2 14DEC2014 2015
2 27AUG2010 2009
;

Then to do the comparison

 

DATA WANT;
    SET TEST;
    IF DATA_VALID >  mdy(12,31,id_year) THEN flag= 1;
        ELSE flag=0;
RUN;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

In order to be able to work with dates, they must be read in using the proper informat to make them dates and not text.

DATA TEST;
    INPUT ID DATA_VALID date9. ID_YEAR;
DATALINES;
1 01APR2012 2013
2 14DEC2014 2015
2 27AUG2010 2009
;

Then to do the comparison

 

DATA WANT;
    SET TEST;
    IF DATA_VALID >  mdy(12,31,id_year) THEN flag= 1;
        ELSE flag=0;
RUN;
--
Paige Miller
Astounding
PROC Star

Why make it complex?  Why not:

 

if year(date_valid) > id_year then flag=1;

else flag=0;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1262 views
  • 1 like
  • 4 in conversation