SAS Procedures

Help using Base SAS procedures
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 6003 views
  • 0 likes
  • 3 in conversation