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

Hi

 

how can I convert this text to a date I can use in SAS.?

 

April 1, 2016

 

currently stored as CHAR $18

 

thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

data want;

   set have;

   Sasdate = input(textdate, anydtdte32.);

   format Sasdate mmddyy10.;

run;

View solution in original post

9 REPLIES 9
Astounding
PROC Star

Typically:

 

sas_date = input(string_date, worddate18.);

 

Then apply a format to SAS_DATE such as:

 

format sas_date yymmdd10.;

 

or

 

format sas_date date9.;

LK357
Obsidian | Level 7

Thanks for the response but im getting the following error...

 

sas_date = input(date, worddate18.);
-----------
48
ERROR 48-59: The informat WORDDATE was not found or could not be loaded

Reeza
Super User

Usually there's an informat for the date, but this doesn't have an informat that I could find.

 

You can create it into a date format that SAS will recognize, I used Date9. format, since it's the most common in SAS.

I use the SUBSTR, SCANx2 function to extract the month, day and year, respectively, and the CATT to concatenate them together.

 

data have;
informat date_char $20.;
input date_char $20.;
cards;
April 1, 2016
December 24, 2016
April 30, 2015
May 18, 2014
November 1, 2012
February 28, 2016
;
run;

data want;
set have;
date_format9 = catt(put(input(scan(date_char, 2), 2.), z2.),
			 substr(date_char, 1, 3),
			  scan(date_char, 3)
			  );
			  
date_num = input(date_format9, date9.);
format date_num date9.;
run;
Astounding
PROC Star

You guys are right!  There's a WORDDATE18 format, but not a WORDDATE18 informat. 

 

Reeza's idea looks good.  I might simplify one part:

 

date_format9 = cats( scan(date_char, 2, ' ,'),  substr(date_char, 1, 3), scan(date_char, 3) );

 

Note that both blanks and commas are being used as delimiters in the first SCAN function.

ballardw
Super User

data want;

   set have;

   Sasdate = input(textdate, anydtdte32.);

   format Sasdate mmddyy10.;

run;

Reeza
Super User

@ballardw Interesting. I tried with ANYDTDTE but didn't specify a width, and that didn't work. I forgot there's a default width 🙂

ballardw
Super User

@Reeza wrote:

@ballardw Interesting. I tried with ANYDTDTE but didn't specify a width, and that didn't work. I forgot there's a default width 🙂


I had done the samething recently with width up to 20. Apparently some of the things anydt reads only work with maximum lengths.

LK357
Obsidian | Level 7
Thank you for this it works fine and is a lot simpler!.
LK357
Obsidian | Level 7

Hi there,

 

Is there a similar solution to extract the time from this text var format $25.

 

12:30PM:Friday:Weekday

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 2507 views
  • 4 likes
  • 4 in conversation