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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 768 views
  • 1 like
  • 4 in conversation