BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RoddyJ
Obsidian | Level 7

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

4 REPLIES 4
Krueger
Pyrite | Level 9

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.

ballardw
Super User

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.

Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 12674 views
  • 2 likes
  • 5 in conversation