Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to convert character type date to date type in SAS

Writer Sophia Terry

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.

2

2 Answers

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.

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.