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