Informat 2-digit year to 4-digit year?

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

Informat 2-digit year to 4-digit year?

Hi guys,

                                                                     01/01/11                                      2001 2001 1911

I'm trying to resolve the raw data(left data set)   02/23/05     to (right data set)         2002 1923 2005

                                                                     03/15/15                                      2003 1915 1915

                                                                     05/09/06                                      2005 2009 2006

Below is my code:

options yearcutoff=1910;

data range;

infile datalines dlm='/';

input year1: yy4.

      year2: yy4.                    How to informat year to make a 2-digit year to be a 4-digit year? Thank you in advance.

      year3: yy4.;

datalines;

01/01/11

02/23/05

03/15/15

05/09/06

;

run;


Accepted Solutions
Solution
‎09-03-2014 11:22 PM
PROC Star
Posts: 7,433

Re: Informat 2-digit year to 4-digit year?

options yearcutoff=1910;

data range (drop=i);

  infile datalines dlm='/';

  format date1-date3 date9.;

  array dates(*) date1-date3;

  array years(3) year1-year3;

  input date1-date3;

  do i=1 to 3;

    dates(i)=mdy(1,1,dates(i));

    years(i)=year(dates(i));

  end;

datalines;

01/01/11

02/23/05

03/15/15

05/09/06

;

View solution in original post


All Replies
Solution
‎09-03-2014 11:22 PM
PROC Star
Posts: 7,433

Re: Informat 2-digit year to 4-digit year?

options yearcutoff=1910;

data range (drop=i);

  infile datalines dlm='/';

  format date1-date3 date9.;

  array dates(*) date1-date3;

  array years(3) year1-year3;

  input date1-date3;

  do i=1 to 3;

    dates(i)=mdy(1,1,dates(i));

    years(i)=year(dates(i));

  end;

datalines;

01/01/11

02/23/05

03/15/15

05/09/06

;

Valued Guide
Posts: 3,208

Re: Informat 2-digit year to 4-digit year?

Chouchou you are learning, I know. This question is meant to get you aware of several things.

1/ dates in SAS are not bound by digits it is only using the number of days since 1jan1960.

2/ reading years / dates wit a two digit / four digit the famous millennium bug (no real bug) is having a sliding window (options yearcutoff). With that the fourdigit expansion

3/ with a delimited file you can specify a length with formats, mostly the length is ignored.

known and conversions (inpt) are implied done.
Run the code to verify this behavior.  

---->-- ja karman --<-----
Super Contributor
Posts: 308

Re: Informat 2-digit year to 4-digit year?

Hello,

Another solution:

data range;

infile datalines ;
input ;

array year{3};

do i=1 to 3;
year{i}=put(mdy(1,1,substr(_infile_,i+2*(i-1),2)),year4.);
end;


datalines;
01/01/11
02/23/05
03/15/15
05/09/06
;

drop i;
run;

Contributor
Posts: 68

Re: Informat 2-digit year to 4-digit year?

Hi Loko,

Can you please elaborate  _infile_,i+2*(i-1),2 a little bit more?

I would be greatly appreciate it.

Thank you.

Super User
Super User
Posts: 7,719

Re: Informat 2-digit year to 4-digit year?

I think its a bit dangerous, for instance your test data:

01/01/11                                      2001 2001 1911

What makes 01 be 2001, and 11 be 1911?  Surely it could as easily be 2011.  I would check the souce (raw data) and fix that.  E.g. if from database is there a spec which details what that data is, if Excel check back with person sending it and ask them to provide full information etc.

Respected Advisor
Posts: 3,787

Re: Informat 2-digit year to 4-digit year?

I believe this is an example of how YEARCUTOFF works,  in which case 11 becomes 2011 because of the OPTIONS YEARCUTOFF=1910; 

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 501 views
  • 8 likes
  • 6 in conversation