BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UcheOkoro
Lapis Lazuli | Level 10

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.

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

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;

jimbarbour_0-1602013291503.png

 

Jim

View solution in original post

6 REPLIES 6
jimbarbour
Meteorite | Level 14

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

jimbarbour
Meteorite | Level 14

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;

jimbarbour_0-1602013291503.png

 

Jim

UcheOkoro
Lapis Lazuli | Level 10

Thank you so much, Jimbarbour. I am grateful.

jimbarbour
Meteorite | Level 14

@UcheOkoro,

 

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

UcheOkoro
Lapis Lazuli | Level 10
I will verify to see which is the correct time. Thank you.
Kurt_Bremser
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 530 views
  • 2 likes
  • 3 in conversation