Hi all,
I'm trying to convert a numeric type to a date type.
My original dataset has a date type variable, I set another variable equal to this and it returns a numeric type. I want this new variable as a date type like the original. I've tried solutions from other posts but the result of those returns the date in the format I want, but as a character type. Can anyone help?
Here's the code I'm currently running which returns the date as character, not a date type.
date1 and date2 are date types of the format ddmmmyyy:hh:mm:ss (e.g. 14NOV2019:17:01:01)
x_time is returned as a numeric (e.g. 0123456789)
final_date is a character type (e.g. "14NOV2019:17:01:01")
data times;
if date1 = . then x_time = date2;
else x_time = date1;
final_date = put(x_time,datetime20.);
format final_date datetime20.;
run;
Hope this explains the problem, thanks in advance for your help and feedback!
Likely all you need to do is apply the format.
data times; if date1 = . then final_date = date2; else final_date = date1; format final_date datetime20.; run;
NOTE: The PUT function always creates character output. That the purpose of the Put function.
All SAS Date, Datetime and Time values are numeric. Dates are number of days since 1 Jan 1960, Datetimes are number of seconds since 1 Jan 1960 and Time values are number of seconds from midnight.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
Maybe this will help explain the difference between PUT() and INPUT() but you are converting it from NUMERIC to CHAR by using the PUT() statement.
Likely all you need to do is apply the format.
data times; if date1 = . then final_date = date2; else final_date = date1; format final_date datetime20.; run;
NOTE: The PUT function always creates character output. That the purpose of the Put function.
All SAS Date, Datetime and Time values are numeric. Dates are number of days since 1 Jan 1960, Datetimes are number of seconds since 1 Jan 1960 and Time values are number of seconds from midnight.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
SAS date (and datetime and time) variables are numeric, and have certain formats attached to them to display times and dates in human-readable form. Internally, they store counts of days or counts of seconds.
If you store a formatted string into a character variable with this:
final_date = put(x_time,datetime20.);
then this
format final_date datetime20.;
is not necessary and will cause an ERROR, as you can't attach a numeric format to a character variable.
SAS only has two data types. Fixed length character strings and floating point numbers. Note that a SAS format is just instructions for how to convert the values to text when displaying them and does not define a new type of variable.
If sounds like you have a numeric variable that has a format attached to it. When you make the new variable just the values are copied.
You just need to attach a format to the new variable so that it will look to you like a date. Use the same format that was attached to the original variable if you want the values to display in the same way.
data times;
x_time = coalesce(date1,date2);
format x_time datetime20.;
run;
You might want to change your variable names. From the format you have attached it appears that all three of those variables contain numbers that represent DATETIME values. Yet your names imply that two of them have DATE values and the third as a TIME value.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.