DATA Step, Macro, Functions and more

Convert WEEKDATX Character to Numeric

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Convert WEEKDATX Character to Numeric

Hello. My date variable looks like this in the dataset:

 

Date_Delivery

Tue Aug 15, 2017 18:43:00 EDT

Mon Sep 11, 2017 10:13:00 EDT

Tue Aug 29, 2017 23:29:00 EDT

 

Data want -- three variables:

Date                        Day of Week            Time

08/15/2017                  Tue                     6:43 PM

09/11/2017                  Mon                    10:13 AM

08/29/2017                  Tue                      11:29 PM

 

Ive tried many different codes on the SAS boards, but none are working. Suggestions for codes? And can you explain what the code is doing? Thanks!


Accepted Solutions
Solution
‎09-20-2017 12:21 AM
Frequent Contributor
Posts: 100

Re: Convert WEEKDATX Character to Numeric

data want;
 set have;
length day $3;
day=scan(date,1,"");
dt=compress(substr(date,4,13),",");
daten=input(catx(scan(dt,1," "),scan(dt,2," "), scan(dt,3," ")),date9.);
time=input(scan(date,-2,""),time8.);
format time timeampm10. daten mmddyy10.;
run;

Review the functions indicated in the program.  Change the data and varaibles accordingly.

View solution in original post


All Replies
Frequent Contributor
Posts: 100

Re: Convert WEEKDATX Character to Numeric

[ Edited ]

Try this.....

proc format;
  value dow
1='Sun'
2='Mon'
3='Tue'
4='Wed'
5='Thu'
6='Fri'
7='Sat'
;

run;


data WANT;
format datetime_today datetime25.6 date mmddyy10. time timeampm10.;
  datetime_today=datetime();
  date=datepart(datetime_today);
  dow=put(weekday(datepart(datetime_today)),dow.);
  time=timepart(datetime_today);
run;

 

Hope this helps.

Contributor
Posts: 32

Re: Convert WEEKDATX Character to Numeric

Posted in reply to ShiroAmada

Yes, it worked to get the three variables. However, it is giving me only one observation for the current day (today). My other 50 observations from the original dataset are not there. Thoughts?

 

Frequent Contributor
Posts: 100

Re: Convert WEEKDATX Character to Numeric

Replace it using your own data.  I am assuming that your date variable is numeric and not character.

proc format;
  value dow
1='Sun'
2='Mon'
3='Tue'
4='Wed'
5='Thu'
6='Fri'
7='Sat'
;

run;


data WANT;
  set YOUR_DATASET_NAME_HERE;

format datetime_today datetime25.6 date mmddyy10. time timeampm10.;
  *datetime_today=YOUR_SOURCE_VARIABLE;
  date=datepart(YOUR_SOURCE_VARIABLE);
  dow=put(weekday(datepart(YOUR_SOURCE_VARIABLE)),dow.);
  time=timepart(YOUR_SOURCE_VARIABLE);
run;

 

 

Contributor
Posts: 32

Re: Convert WEEKDATX Character to Numeric

Posted in reply to ShiroAmada

It is character. When I enter your code, three varaibles are create but all values are missing and the log gives errors.

Solution
‎09-20-2017 12:21 AM
Frequent Contributor
Posts: 100

Re: Convert WEEKDATX Character to Numeric

data want;
 set have;
length day $3;
day=scan(date,1,"");
dt=compress(substr(date,4,13),",");
daten=input(catx(scan(dt,1," "),scan(dt,2," "), scan(dt,3," ")),date9.);
time=input(scan(date,-2,""),time8.);
format time timeampm10. daten mmddyy10.;
run;

Review the functions indicated in the program.  Change the data and varaibles accordingly.

Contributor
Posts: 32

Re: Convert WEEKDATX Character to Numeric

Posted in reply to ShiroAmada

That's the one. Can you explain what the scan part of the command does?

Thank you for your help.

Frequent Contributor
Posts: 100

Re: Convert WEEKDATX Character to Numeric

Contributor
Posts: 32

Re: Convert WEEKDATX Character to Numeric

Posted in reply to ShiroAmada

Thanks

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 152 views
  • 0 likes
  • 2 in conversation