BookmarkSubscribeRSS Feed
SASforHealth
Obsidian | Level 7
Hi all,

I have a variable that have date and time. Is there a code to identify the day of the week and time of the day from this unformatted variable? Thanks in advance!
14 REPLIES 14
PeterClemmensen
Tourmaline | Level 20

Is the variable chracter or an actual SAS Datetime value?

ed_sas_member
Meteorite | Level 14

 

Hi @SASforHealth 

 

Assuming the variable is a SAS datetime you can do:

data test;
format dt datetime18. time time10.;
dt = "06MAR2020 12:36"dt;
day = weekday(datepart(dt));  
time = timepart(dt);
run;

 Assuming it is a character string:

data test;
format. time time10.;
dt = input("06MAR2020 12:36", datetime18.);
day = weekday(datepart(dt));
time = timepart(dt);
run;
SASforHealth
Obsidian | Level 7

my current variable is Incident_DateTime with a character value of $32000 but the date represents a true sas date and time (ex. value=43323.24039351852). I have attempted both of your codes but I am not having success. I am stuck and not sure how to switch it to a sas date time variable. In your example could you please use my variable name to reduce confusion. thanks so much!

Tom
Super User Tom
Super User

@SASforHealth wrote:

my current variable is Incident_DateTime with a character value of $32000 but the date represents a true sas date and time (ex. value=43323.24039351852). I have attempted both of your codes but I am not having success. I am stuck and not sure how to switch it to a sas date time variable. In your example could you please use my variable name to reduce confusion. thanks so much!


That helps.  I was wondering how you were planning to convert a value like '$32000' in a date time.

Those numbers do NOT look like date time. 

Please explain what date and time the value 43,323.24039351852 is supposed to be?

Is that supposed to be number of days and fractions of a day? 

What date does the number 43,323 represent?

SASforHealth
Obsidian | Level 7

sometime in the past 3 years 2017-2020, i am not sure of the date as this is my only date time variable.

Tom
Super User Tom
Super User

Perhaps it is counting days since 1900 instead of since 1960 like SAS does?

1612  data test;
1613    dt1=dhms(43323.24039351852,0,0,0);
1614    dt2=dt1+'01Jan1900:00:00'dt;
1615
1616
1617    put (_all_) (= comma25.4);
1618    put (_all_) (= datetime25.4);
1619  run;

dt1=3,743,127,970.0000 dt2=1,849,758,370.0000
dt1=12AUG2078:05:46:10.0000 dt2=13AUG2018:05:46:10.0000

If so then that value looks like it represents: 13AUG2018:05:46:10

 

SASforHealth
Obsidian | Level 7

ahh okay not sure why it is doing that, how odd? can you please help me craft the code then to get my Incident_DateTime into normal date, time, and day of week formats?

SASforHealth
Obsidian | Level 7

I used code:


Date2 =DATEPART(Incident_DateTime);
Time= TIMEPART (Incident_DateTime);

now my time is correct and my date is falsely "01JAN1960" any suggestions? My true date values are present date

ballardw
Super User

@SASforHealth wrote:
Hi all,

I have a variable that have date and time. Is there a code to identify the day of the week and time of the day from this unformatted variable? Thanks in advance!

Please help us help you by showing what you have and whether that is a SAS character variable or not.

SASforHealth
Obsidian | Level 7

it is a character value ($32,000.00 )

andreas_lds
Jade | Level 19

@SASforHealth wrote:

it is a character value ($32,000.00 )


Then fix that issue first and transform it to be a more useful sas-datetime-variable.

andreas_lds
Jade | Level 19

@SASforHealth wrote:

how?


Depends on the actual values you have, see the post of @ed_sas_member for a hint on how to convert text to a datetime. You could post example data, so that code could be suggested.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 2554 views
  • 0 likes
  • 6 in conversation