This is very bizarre and I can't figure out for the life of me why but I have some data with birth dates and eligibility dates. The data is originally in a txt file and I am reading in the birth_date as mmddyy10. The eligibility_month data is in numeric format and appears like this:
Birth_date ID Eligibility_Month
01/01/2016 0001 201511
01/01/2016 0001 201512
01/01/2016 0001 201601
01/01/2016 0001 201602
01/01/2016 0001 201603
01/01/2016 0001 201604
01/01/2016 0001 201605
I've tried reading in eligibility_month as yymmn6. and numeric, then converting it to yymmn6. using:
data mom_elig_2016_3;
set mom_eligibility_2016_2; yymm=input(put(eligible_month_yyyymm, 6.), yymmn6.);
format yymm yymmn6.;
informat yymm yymmn6.;
date_my=birth_date;
format date_my yymmn6.;
informat date_my yymmn6.;
run;
The idea is to check if the birth_date = an equivalent entry in the eligibility_month. I reformatted birth_date into the same format.
For some reason, SAS recognizes SOME of the date_my and yymm as equivalent but not others.
data aa1; set mom_elig_2016_3;
if date_my=yymm; run;
So for example, this is inaccurate because I see records where date_my = yymm clearly equals each other but isn't picked up. I must be missing something to do with formatting but I can't figure out what it is.
data have;
length Eligibility_Month 6.;
input Birth_date ddmmyy10. ID Eligibility_Month ;
format birth_date ddmmyy10.;
datalines;
01/01/2016 0001 201511
01/01/2016 0001 201512
01/01/2016 0001 201601
01/01/2016 0001 201602
01/01/2016 0001 201603
11/11/2019 0002 201911
;
run;
data want;
set te;
o_mon=month(Birth_date);
o_yr=year(Birth_date);
if length(strip(put(o_mon,best.))) eq 1 then
birht_date_n=strip(strip(put(o_yr,best.))||'0'||strip(put(o_mon,best.)));
else birht_date_n=strip(strip(put(o_yr,best.))||strip(put(o_mon,best.)));
if input(birht_date_n,6.) eq Eligibility_Month then output;
drop o_mon o_yr birht_date_n;
run;
Formatting doesn't change the underlying value. Two dates can be formatted to look identical, but the underlying values are not equal, and so
if date_my=yymm;
would fail.
So the idea of formatting things to look the same won't help here.
The act of turning eligible_month_yyyymm to character and then back to numeric also seems unnecessary. If eligible_month is numeric string, you could turn it into an actual sas day via the INPUT command. If it is numeric, something like this would work:
yymm = mdy(mod(eligibility_month,100),1,floor(eligibility_month/100));
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.