BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eabc0351
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ShiroAmada
Lapis Lazuli | Level 10
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

8 REPLIES 8
ShiroAmada
Lapis Lazuli | Level 10

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.

eabc0351
Quartz | Level 8

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?

 

ShiroAmada
Lapis Lazuli | Level 10

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;

 

 

eabc0351
Quartz | Level 8

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

ShiroAmada
Lapis Lazuli | Level 10
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.

eabc0351
Quartz | Level 8

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

Thank you for your help.

eabc0351
Quartz | Level 8

Thanks

sas-innovate-2024.png

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.

 

Register now!

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
  • 8 replies
  • 1094 views
  • 0 likes
  • 2 in conversation