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.
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;
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;
Why make it complex? Why not:
if year(date_valid) > id_year then flag=1;
else flag=0;
Or even shorter:
flag = year(date_valid) > id_year;
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!
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.