how to convert char to a date

Reply
Frequent Contributor
Posts: 134

how to convert char to a date

Hi

I have a column of type Character that holds the date in the following format

Year-Month-day

data looks something like this:

2012-1-01 (first of jan)

2012-2-01 (1st of Feb and so on)

May I know how to convert it into a date format?

Thanks

Respected Advisor
Posts: 4,930

Re: how to convert char to a date

Take advantage of the formats available in SAS :

data have;
date = "2012-1-01";
output;
date = "2012-2-01";
output;
run;

data want;
set have;
numDate = input (date, yymmdd12.);
format numDate yymmdd10.;
run;

proc print; run;

PG

PG
Super User
Posts: 11,343

Re: how to convert char to a date

If in an existing SAS dataset the variable can't be "converted" from string to numeric (date and such are numeric). A new variable from your existing variable can be added using

data new;

     set old;

     Sasdate = input(yourstringvariablename, anydtdte12.);

     /* you probably want to assign a display format to make the values understandable */

    format sasdate mmddyy10.; /* as one example*/

run;

If you are reading the variable from a file you can apply the informat when read by using something like:

Informat datevaraible anydtdte12; before the input statement.

Occasional Contributor
Posts: 6

Re: how to convert char to a date

If you are really stuck with delivering something on the basis of this I shall suggest to export this column to excel, make your desired changes and import.

-Dipanjan

Ask a Question
Discussion stats
  • 3 replies
  • 532 views
  • 1 like
  • 4 in conversation