Programming the statistical procedures from SAS

Help in Conversion of Format for Date variables

Reply
Occasional Contributor
Posts: 8

Help in Conversion of Format for Date variables

Hi ,

Please help in converting character variable (Start Date) to date format (mm/dd/yyyy). I have given an example below

Start date $

11/1/2009 0:00:00

4/2/2009 0:00:00

26/1/2009 0:00:00

26/11/2009 0:00:00

26/11/2009 12:00:00 AM

1/11/2009 0:00:00

1/11/2009 12:00:00 AM

Thank you for your help

Esteemed Advisor
Posts: 7,056

Help in Conversion of Format for Date variables

Here is one way:

options datestyle=dmy;

data have;

  informat Start_date $30.;

  input Start_date &;

  cards;

11/1/2009 0:00:00

4/2/2009 0:00:00

26/1/2009 0:00:00

26/11/2009 0:00:00

26/11/2009 12:00:00 AM

1/11/2009 0:00:00

1/11/2009 12:00:00 AM

;

data want (drop Start_date_in);

  set have (rename=(Start_date=Start_date_in));

  format Start_date mmddyy10.;

  Start_date=input(scan(Start_date_in,1,' ','r'), ddmmyy10.);

run;

I would have thought one could do it by inputting the entire datetime string, and using the anydtdtm informat, but it didn't work correctly for the 3rd from the last record.

Ask a Question
Discussion stats
  • 1 reply
  • 183 views
  • 0 likes
  • 2 in conversation