7

I want to convert a String to Date in SAS, I tried:

data _null_; 
   monyy = '05May2013'; 
   date = input(substr(strip(monyy),1,9),yymmdd.);;
   put date=date9.; 
   run;

But it did not work. Can this be done?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
SAS_learner
  • 521
  • 1
  • 13
  • 30

7 Answers7

15

Formats like

date9. 

or

mmddyy10. 

are not valid for input command while converting text to a sas date. You can use

Date = input( cdate , ANYDTDTE11.);

or

Date = input( cdate , ANYDTDTE10.); 

for conversion.

VMai
  • 10,156
  • 9
  • 25
  • 34
Mitul
  • 151
  • 1
  • 2
11

You don't need substr or strip.

input(monyy,date9.);
data _null_
  • 8,534
  • 12
  • 14
3

As stated above, the simple answer is:

date = input(monyy,date9.);

with the addition of:

put date=yymmdd.;

The reason why this works, and what you did doesn't, is because of a common misunderstanding in SAS. DATE9. is an INFORMAT. In an INPUT statement, it provides the SAS interpreter with a set of translation commands it can send to the compiler to turn your text into the right numbers, which will then look like a date once the right FORMAT is applied. FORMATs are just visible representations of numbers (or characters). So by using YYMMDD., you confused the INPUT function by handing it a FORMAT instead of an INFORMAT, and probably got a helpful error that said:

Invalid argument to INPUT function at line... etc...

Which told you absolutely nothing about what to do next.

In summary, to represent your character date as a YYMMDD. In SAS you need to:

  1. change the INFORMAT - date = input(monyy,date9.);
  2. apply the FORMAT - put date=YYMMDD10.;
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
1

Try

data _null_; 
   monyy = '05May2013'; 
   date = input(substr(strip(monyy),1,9),date9.);
   put date=date9.; 
   run;
Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
1

input(char_val, date9.);

You can consider to convert it to word format using input(char_val, worddate.)

You can get a lot in this page http://v8doc.sas.com/sashtml/lrcon/zenid-63.htm

Santhosh
  • 1,771
  • 1
  • 15
  • 25
1

This code helps:

data final; set final;

first_date = INPUT(compress(char_date),date9.); format first_date date9.;

run;

I personally have tried it on SAS

0
input(char_val,current_date_format);

You can specify any date format at display time, like set char_val=date9.;

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126