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

Hello, 

 

I need to parse out the date from a character string. The format of the variable (called "LogDate") is like this: 2/16/2019 12:00:00 AM. I just need the date part (2/16/2019). 

 

My previous code worked, but it only parses out dates from 2019. Now that it is 2020, I need to include those dates as well. This is what I was using before: 

data providers;
set dsn5;

log_part=substr(logdate, 1, index(logdate, '2019')+3);
log_date=input(log_part, mmddyy10.);

log_date=datepart(logdate);

format log_date mmddyy10.;
drop log_part logdate;

run;

 

The goal is to have the variable "log_date" be the date parsed out from the character string and then formatted using mmddyy10. Please let me know what you would suggest - I couldn't figure it out using other queries from the SAS community 😕

 

TIA! 

Christine 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @fordcr2  Keeping it simple

 

data test;
char_date='2/16/2019 12:00:00 AM';
sas_date=input(char_date,mmddyy10.);
format sas_date mmddyy10.;
run;

 

Basically, the idea is read 10 bytes of char with a date informat and letting the informat to convert the non-standard date to a SAS date that is numeric. Once that is done, you can format however you want 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @fordcr2  Keeping it simple

 

data test;
char_date='2/16/2019 12:00:00 AM';
sas_date=input(char_date,mmddyy10.);
format sas_date mmddyy10.;
run;

 

Basically, the idea is read 10 bytes of char with a date informat and letting the informat to convert the non-standard date to a SAS date that is numeric. Once that is done, you can format however you want 

fordcr2
Obsidian | Level 7

Perfect - thank you!