Hello!
Please, I need help with date and time format. I have a variable labelled "Med_order_datetime" with observations that looks like this "1758828180".
I also have another variable labelled "medgiven" which has observations like "20356" and a "medgiven_sec" variable with observations that look like this "20356.807639".
Please, I need help converting them to date and time.
Thank you.
Here's some code. I'm copying the values verbatim from your original post. Look at the results below that. Notice that Med_Order_DateTime is just that, a date-time value. I didn't change the value. I just formatted it. DateTime values are stored as a count of seconds since January 1, 1960. You can format them any way you like. The format is used to display them so the human eye can read them, but the internal value is a number representing the number of seconds. Format them as you please, but the underlying value will not change.
Dates and times are similar. Notice they icons SAS uses. MedGiven_Sec has a little clock, for example, indicating that SAS will handle this as a time value. I've formatted them with date and time formats, respectively, but they are both stored as numbers. Date values, for example, are a count of the number of days since January 1, 1960.
DATA Dates_And_Times;
Format Med_order_datetime DATETIME21.2;
Format MedGiven DATE9.;
Format MedGiven_Sec TIME8.;
Med_order_datetime = 1758828180;
MedGiven = 20356;
medgiven_sec = 20356.807639;
RUN;
Jim
Actually, if those values are in a SAS data set, they already look like dates and times. All you need to do is format them for the human eye.
Give me a moment and let me create an example...
Jim
Here's some code. I'm copying the values verbatim from your original post. Look at the results below that. Notice that Med_Order_DateTime is just that, a date-time value. I didn't change the value. I just formatted it. DateTime values are stored as a count of seconds since January 1, 1960. You can format them any way you like. The format is used to display them so the human eye can read them, but the internal value is a number representing the number of seconds. Format them as you please, but the underlying value will not change.
Dates and times are similar. Notice they icons SAS uses. MedGiven_Sec has a little clock, for example, indicating that SAS will handle this as a time value. I've formatted them with date and time formats, respectively, but they are both stored as numbers. Date values, for example, are a count of the number of days since January 1, 1960.
DATA Dates_And_Times;
Format Med_order_datetime DATETIME21.2;
Format MedGiven DATE9.;
Format MedGiven_Sec TIME8.;
Med_order_datetime = 1758828180;
MedGiven = 20356;
medgiven_sec = 20356.807639;
RUN;
Jim
Thank you so much, Jimbarbour. I am grateful.
You're very welcome. Be a little careful with medgiven_sec. It bothers me that it's different than the other values. Is there a way to validate that it is correctly rendered? Can we double check to see if it is giving us the correct time?
Jim
That 20356.807639 could be the result of an import of a Excel datetime, where '30dec1899'd was added, but the Excel time (fraction of a day) was not converted.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.