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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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