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

I'm trying to parse dates and times in sas.

So for instance I'd like to be able to parse this date 1/20/2012, I tried substr to get what I'm looking for along the lines of:

Month = substr(Date, 1,1);

Day = substr(Date,3,4);

Year = substr(Date,6,9);

This did not work

I would also like to parse time as well: 5:00:00 PM, it seems that substr doesn't work on this as well.

such that I'd get

Hour = 5

Minute = 0

Any suggestions?

1 ACCEPTED SOLUTION

Accepted Solutions
SteveNZ
Obsidian | Level 7

Depends if number is stored as SAS date with a format on it. If it's stored as a character string then:

data want ;

length day month $2 year $4 ;

set have ;

day = scan(date,2,'/') ;

month = scan(date,1,'/') ;

year = scan(date,3,'/') ;

run ;

If as a date then:

data want ;

set have ;

day = day(date) ;

month = month(date) ;

year = year(date) ;

run ;

Similarly:

data want ;

set have ;

hour = hour(time) ;

minute = minute(time) ;

run ;

data want ;

     length hour minute $2 ;

set have ;

hour = scan(time,1,':') ;

minute = scan(time,2,':') ;

run ;

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

The third argument to substr is LENGTH.  You would do better with SCAN function.  Or better still read the dates and times and SAS dates and times then you get a suite of very nice functions to do all kinds if things.  Like pick out the day month or year.

SteveNZ
Obsidian | Level 7

Depends if number is stored as SAS date with a format on it. If it's stored as a character string then:

data want ;

length day month $2 year $4 ;

set have ;

day = scan(date,2,'/') ;

month = scan(date,1,'/') ;

year = scan(date,3,'/') ;

run ;

If as a date then:

data want ;

set have ;

day = day(date) ;

month = month(date) ;

year = year(date) ;

run ;

Similarly:

data want ;

set have ;

hour = hour(time) ;

minute = minute(time) ;

run ;

data want ;

     length hour minute $2 ;

set have ;

hour = scan(time,1,':') ;

minute = scan(time,2,':') ;

run ;

steppermotor
Calcite | Level 5

SteveNZ,


Thanks for your response the code you provided work well. It accomplished what I needed it to. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 5250 views
  • 0 likes
  • 3 in conversation