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!
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.
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.
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?
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;
It is character. When I enter your code, three varaibles are create but all values are missing and the log gives errors.
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.
That's the one. Can you explain what the scan part of the command does?
Thank you for your help.
Thanks
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.