bat
bat

Reputation: 23

How to convert string to date in SAS?

I have a table in SAS where in one column, the date is stored (e.g. "2005/10"). How do I have to convert this to a SAS data format?

Among many other tries, I tried this code:

data test;

        format date YYMMS.;

        date = input(ObservationMonth, YYMMS.);

        put date=date9.;

run;

Upvotes: 2

Views: 3498

Answers (3)

bat
bat

Reputation: 23

you're fantastic, guys! Thank you so much, with a "set" statement it works fine!

Upvotes: 0

user2877959
user2877959

Reputation: 1792

You could just use the anydtdte. informat.

data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;

This informat detects most date formattings and converts the character-stored value to a SAS date.

Upvotes: 1

Allan Bowe
Allan Bowe

Reputation: 12691

One way is to use substr() and mdy() to extract the date components:

data _null_;
  ObservationMonth ="2005/10";
  date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
  put date = date9.;
run;

Another option is to use the anydtdte informat (note that results may differ depending on your locale):

data _null_;
  ObservationMonth ="2005/10";
  date =input(ObservationMonth,anydtdte.);
  put date = date9.;
run;

Yet another option is to modify the input to enable use of the YYMMDDw. informat:

data _null_;
  ObservationMonth ="2005/10";
  date =input(ObservationMonth!!'/01', YYMMDD10.);
  put date = date9.;
run;

Upvotes: 0

Related Questions