Hello,
Please help me to change the no 198704 to a SAS date, where 1987 is Year and 04 is month.
Actually I want to calculate the difference (L.O.R) b/w to Date2 and Date1 in months.
Please tell me where is I am mistaking.
data abc;
set ab1;
Date1_changed = put(input(put(Date1,6.),yymmn6.),yymmn6.);
Date2_changed = put(input(put(Date2,6.),yymmn6.),yymmn6.);
LOR = intck('Months',date1, date2);
run;
Set - ab1
Date1 | Date2 |
198704 | 201106 |
200601 | 201103 |
198709 | 200901 |
201307 | 201307 |
201310 | 201310 |
199411 | 200412 |
199308 | 200306 |
200607 | 201304 |
A SAS numeric date has to have all three parts - year, month, and day. Irrespective of if you format it as month/year only it still needs the day. This is because the value behind is actually number of days since the cuttoff. So you would need something like:
data want; set ab1; date_1=mdy(input(substr(date,5,2),best.),1, input(substr(date,1,4),best.)); format date_1 yymmn6.; run;
Or more simply using an informat:
data want; set ab1; date_1=input(date,yymmn6.); format date_1 yymmn6.; run;
Not tested, post test data in the form of a datastep in future!
The conversion is easier done in a strictly numeric manner:
data test;
number = 198704;
date = mdy(mod(number,100),1,int(number/100));
format date yymmddd10.;
run;
Avoids all the fiddling around with formats and informats.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.