How to create a weekday variable from a datetime19. variable?

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

How to create a weekday variable from a datetime19. variable?

Hello I have a timeseries data set with a date variable of the format datetime19.

I would like to create a new variable using the date variable that will contain only the weekday.

An example observation of datetime is: '11JAN2012:07:01:26'

I tried using the following code (datetime19. is called date_G_)

******create a new date variable containing only the date and will be readable by the weekday command****

data neo.eurousdjp15; set neo.eurousdjp15;

date=substr(date_G_,1,9);

run;

********run the weekday command************;

data neo.eurousdjp15; set neo.eurousdjp15;

wkday = weekday(date);

run;

When I check the results I see many days being missallocated, thus wednesdays for example counted as sundays.

Any ideas how to solve this?

Thanks

Neo


Accepted Solutions
Solution
‎11-14-2013 10:50 AM
Super User
Posts: 17,864

Re: How to create a weekday variable from a datetime19. variable?

Use the datepart function to get the date, rather than substr. I'm surprised you're not getting an error in your log. Unless your date is actually a character variable instead of a numeric variable.

weekday(datepart(date_g_))

View solution in original post


All Replies
Solution
‎11-14-2013 10:50 AM
Super User
Posts: 17,864

Re: How to create a weekday variable from a datetime19. variable?

Use the datepart function to get the date, rather than substr. I'm surprised you're not getting an error in your log. Unless your date is actually a character variable instead of a numeric variable.

weekday(datepart(date_g_))

Contributor
Posts: 57

Re: How to create a weekday variable from a datetime19. variable?

perfect, it worked! thanks for the timely reply!

Best

Neo

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 318 views
  • 0 likes
  • 2 in conversation