Home > Mobile >  How to convert character type date to date type in SAS
How to convert character type date to date type in SAS

Time:10-11

I have a character variable in the form of "Jan-17" in my table and would like to convert it to date type. Does anyone what kind of date form this is and how I could go about converting it? Thank you.

CodePudding user response:

SAS date variables are stored as number of days since Jan 1, 1960. For a variable that is Month and Year only, you'd need to pick a day - many choose the 1st, some the 15th or 30th/31st, depending on your business rules.

Either way, you can use the input function as a starting place. You need to know the informat - the instructions for how to translate characters to numbers. In your case, MONYY6. will likely work, though you can also try ANYDATEDTE. to let SAS guess the date informat to use.

Then, if needed, you can adjust the date it chose based on your business rules using the intnx function, which allows you to adjust it to the 15th or end of month if you prefer.

CodePudding user response:

Use the INPUT() function with the correct informat.

data have;
date_char = 'Jan-17';
run;

data want;
set have;
date_dt = input(date_char, monyy6.);

format date_dt date9.;
run;
01JAN2017
  • Related