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?
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 ;
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.
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 ;
SteveNZ,
Thanks for your response the code you provided work well. It accomplished what I needed it to.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.