Help using Base SAS procedures

Text dates to a Sas date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Text dates to a Sas date

Hi

 

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

 

April 1, 2016

 

currently stored as CHAR $18

 

thanks


Accepted Solutions
Solution
‎07-14-2016 05:42 AM
Super User
Posts: 10,500

Re: Text dates to a Sas date

data want;

   set have;

   Sasdate = input(textdate, anydtdte32.);

   format Sasdate mmddyy10.;

run;

View solution in original post


All Replies
Super User
Posts: 5,083

Re: Text dates to a Sas date

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.;

Occasional Contributor
Posts: 12

Re: Text dates to a Sas date

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

Super User
Posts: 17,829

Re: Text dates to a Sas date

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;
Super User
Posts: 5,083

Re: Text dates to a Sas date

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.

Solution
‎07-14-2016 05:42 AM
Super User
Posts: 10,500

Re: Text dates to a Sas date

data want;

   set have;

   Sasdate = input(textdate, anydtdte32.);

   format Sasdate mmddyy10.;

run;

Super User
Posts: 17,829

Re: Text dates to a Sas date

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

Super User
Posts: 10,500

Re: Text dates to a Sas date


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 Smiley Happy


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

Occasional Contributor
Posts: 12

Re: Text dates to a Sas date

Thank you for this it works fine and is a lot simpler!.
Occasional Contributor
Posts: 12

Re: Text dates to a Sas date

Hi there,

 

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

 

12:30PM:Friday:Weekday

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 429 views
  • 4 likes
  • 4 in conversation