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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.