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! 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 555 views
  • 0 likes
  • 2 in conversation