Sara A.
Sara A.

Reputation: 95

SAS date swap year and day

I am working with a dataset containing a date variable with the format MMDDYY10.. The problem is, that the day, month and the year have been swapped.

The data set as it looks now:

Obs Date
1   11/01/1931
2   11/06/1930
3   12/02/2003
4   12/07/2018

What I would like is a date variable with the format DDMMYY10., or a similar:

Obs Date
1   31/01/2011
2   30/06/2011
3   03/02/2012
4   18/07/2012

Observation 1 is hence written as the 1st of November 1931, but really it is the 31st of January 2011.

Does anyone know how I can change this?

Upvotes: 1

Views: 536

Answers (1)

Tom
Tom

Reputation: 51566

Looks like you read the original raw data using the wrong INFORMAT. Most likely you had data in YYMMDD format and you read it as MMDDYY. You can use the PUT() and INPUT() functions to attempt to reverse it.

data have ;
  input date mmddyy10.;
  newdate = input(put(date,mmddyy6.),yymmdd6.);
  format date newdate yymmdd10. ;
  put (date newdate) (=);
cards;
11/01/1931
11/06/1930
12/02/2003
12/07/2018
;;;;

Results:

date=1931-11-01 newdate=2011-01-31
date=1930-11-06 newdate=2011-06-30
date=2003-12-02 newdate=2012-02-03
date=2018-12-07 newdate=2012-07-18

Upvotes: 1

Related Questions