Leap Year date conversion

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Leap Year date conversion

In this module I am converting mainframe dates to sas dates

I need to set any invalid date to a null value.  How do I account for leap year for the value in the module of 229?  In this example in 2016 we have a Feb 29th so this would result in a null value when there should be a date.

ARRAY MF_DATE(2) DATE1 DATE2;

ARRAY SAS_DATE(2) SASDATE1 SASDATE2;

FORMAT SASDATE1 SASDATE2 MMDDYY10.;

DO I=1 TO 1;

  **** TO CARE FOR INVALID AND NULL DATES *****;

IF MF_DATE(I) IN (.,0,9999999) THEN SAS_DATE(I)=.;

ELSE IF MOD(MF_DATE(I),10000) > 1231 THEN SAS_DATE(I)= .;

ELSE IF MOD(MF_DATE(I),10000) IN

(229,230,231,431,631,931,1131) THEN SAS_DATE(I)= .;

ELSE IF MOD(MF_DATE(I),100) = 0 THEN SAS_DATE(I)=.;

ELSE IF MOD(MF_DATE(I),100) > 31 THEN SAS_DATE(I)=.;

ELSE SAS_DATE(I)=INPUT(PUT(MF_DATE(I)+19000000,8.),YYMMDD8.);

END;

Thanks in advance.


Accepted Solutions
Solution
‎06-05-2015 08:25 AM
Trusted Advisor
Posts: 1,496

Re: Leap Year date conversion

If the mainframe date is 1150525 (century, 2 digit year, 2 digit month, 2 digit day)

then

century=floor(MF_DATE(I)/1000000);

year=mod(floor(MF_DATE(I)/10000),100)+(century+19)*100;


View solution in original post


All Replies
Trusted Advisor
Posts: 1,496

Re: Leap Year date conversion

Use the MDY function, this will return missings if the inputs are not a valid date.


monthday=MOD(MF_DATE(I),10000);

month=floor(monthday/100);

day=mod(monthday,100);

year= .... ;

sas_date(i)=mdy(month,day,year);

Also, work with SAS date values, as shown above, instead of creating your own strings/integers to represent dates.

Contributor
Posts: 66

Re: Leap Year date conversion

I am sorry I am not understanding how or where that fits into my array, or even if it does.

Thanks

Trusted Advisor
Posts: 1,496

Re: Leap Year date conversion

It's partial code. I didn't rewrite everything for you.

It goes between

DO I=1 TO 1;

and

END;

Contributor
Posts: 66

Re: Leap Year date conversion

I think I have it working except for the year part.  The mainframe date looks like 1150525, it is century, 2 digit year, 2 digit month, 2 digit day

so the year is looking at the last 2 digits, in this example my formatted date looks like 05/25/1925.

how do I care for the century digit?

Thanks you all for your help.

Solution
‎06-05-2015 08:25 AM
Trusted Advisor
Posts: 1,496

Re: Leap Year date conversion

If the mainframe date is 1150525 (century, 2 digit year, 2 digit month, 2 digit day)

then

century=floor(MF_DATE(I)/1000000);

year=mod(floor(MF_DATE(I)/10000),100)+(century+19)*100;


Contributor
Posts: 66

Re: Leap Year date conversion

Thank you so much, this did exactly what I needed.

Super User
Super User
Posts: 6,330

Re: Leap Year date conversion

So to convert 7 digit mainframe dates of the form cYYMMDD, where c=1 means 1900, to normal 8 digit dates of the form CCYYMMDD you need to add 18,000,000.

If you want to silently convert the invalid ones to missing then use the ?? modifier on the INFORMAT.

data _null_;

   input mf ;

   date = input(put(mf+18E6,8.),??yymmdd8.);

   put mf= date= yymmdd10. ;

cards;

1150525

2150525

2150228

2010229

;;;;

mf=1150525 date=1915-05-25

mf=2150525 date=2015-05-25

mf=2150228 date=2015-02-28

mf=2010229 date=.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 426 views
  • 0 likes
  • 3 in conversation