BookmarkSubscribeRSS Feed
vishalrajpoot3
Obsidian | Level 7

 

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

Date1Date2
198704201106
200601201103
198709200901
201307201307
201310201310
199411200412
199308200306
200607201304

 

 

 

 

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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!

Kurt_Bremser
Super User

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.

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 943 views
  • 0 likes
  • 3 in conversation