DATA Step, Macro, Functions and more

Character Date Conversion

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 123
Accepted Solution

Character Date Conversion

[ Edited ]

I've imported an excel sheet into SAS EG using the import wizard and the date values are character values that look like 2013/03/01. I need to convert these to SAS Date values, but when I try to use input the values just come out as missing values. How can I properly convert these character dates to SAS date values? Thanks.


Accepted Solutions
Solution
‎01-30-2017 05:54 PM
PROC Star
Posts: 7,471

Re: Character Date Conversion

Posted in reply to JediApprentice

You didn't show the code you used. The following, using the anydtdte informat worked for me:

 

data have;
  input date $10.;
  cards;
2013/03/01
2012/04/02
;
data want;
  set have (rename=(date=_date));
  format date date9.;
  date=input(_date,anydtdte10.);
run;

HTH,

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Solution
‎01-30-2017 05:54 PM
PROC Star
Posts: 7,471

Re: Character Date Conversion

Posted in reply to JediApprentice

You didn't show the code you used. The following, using the anydtdte informat worked for me:

 

data have;
  input date $10.;
  cards;
2013/03/01
2012/04/02
;
data want;
  set have (rename=(date=_date));
  format date date9.;
  date=input(_date,anydtdte10.);
run;

HTH,

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 123

Re: Character Date Conversion

@art297 This does indeed turn it into a SAS date, however ultimately what I'm trying to do after the conversion is subset a dataset by an expression:

 

proc sql noprint;                              
 select ID into :ID_List_2010 separated by ' '
 from EGTASK.'ID FROM DATES_UPDATED'n
 where from_date < '01jan2011'd;
quit;

But it is giving me the error:

 

Expression using less than  (<) has components that are of different data types.

 

Sorry, I did not specify before that I wanted to do a condition on dates.

PROC Star
Posts: 7,471

Re: Character Date Conversion

Posted in reply to JediApprentice

I'd think that your log shows some other error before that. The following worked for me:

 

  input id $ date $10.;
  cards;
a 2013/03/01
b 2010/03/01
c 2010/04/02
;

data want;
  set have (rename=(date=_date));
  format from_date date9.;
  from_date=input(_date,anydtdte10.);
run;

proc sql noprint;                              
 select ID into :ID_List_2010 separated by ' '
   /* from EGTASK.'ID FROM DATES_UPDATED'n*/
   from want
     where from_date < '01jan2011'd
  ;
quit;

Art, CEO, AnalystFinder.com

Super User
Posts: 3,252

Re: Character Date Conversion

Posted in reply to JediApprentice

Try defining the date columns in Excel as type DATE to begin with, then re-import.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 134 views
  • 0 likes
  • 3 in conversation