Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String to Date value in SAS?

Tags:

sas

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?

like image 547
SAS_learner Avatar asked Mar 26 '13 20:03

SAS_learner


3 Answers

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.

like image 145
Mitul Avatar answered Nov 11 '22 09:11

Mitul


You don't need substr or strip.

input(monyy,date9.);
like image 41
data _null_ Avatar answered Nov 11 '22 09:11

data _null_


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.;
like image 30
user3420862 Avatar answered Nov 11 '22 07:11

user3420862