- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SteveNZ,
Thanks for your response the code you provided work well. It accomplished what I needed it to.