DATA Step, Macro, Functions and more

Character to SAS Date conversion

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Character to SAS Date conversion

Hello members,

I'm a new member joined. I know the question I'm going to ask might have been asked n number of times. But so far I'm unable to fix the problem.

Problem:

I'm trying to convert SAS Date in DHMS format to SAS Date Date9 format. The date came in from SQL server and in DDMMMYYYY:00:00:00.000 format.

The date I have is 10JAN2011. I have converted to numeric and to character and I get the value 18637 in character format. But then, I'm unable to convert to date9 format. All I get is missing values with error message as "Invalid argument to function Input". I also tried with sasdatefmt in standard syntax, but the code is not working

Code :

data want;

set have;

dt= input(put(datepart(Date),5.),ddmmyy10.);

format dt Date9.;

run;

Thank you.


Accepted Solutions
Solution
‎02-01-2013 11:00 AM
Super Contributor
Posts: 334

Re: Character to SAS Date conversion

Not sure its clear what you are trying to do.

If all you need is the date to show as 10Jan2011 then you do not need to use the put function (just the datepart) and just associate the format with the variable. If you are converting to a character variable then I think you need to just replace the 5. format in the put with the date9. format.

EJ

EDIT:

Sample code to illustrate

data temp;

  x = 18637;

  format x date9.;

  x2= put(x,date9.);

  y='10Jan2011 12:00:00'dt;

  y2 = datepart(y);

  format y2 date9.;

  y3 = put(y2,date9.);

run;

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Character to SAS Date conversion

18637 is 10Jan2011 nothing else to do except associate a format.

17         data _null_;

18            x = 18637;

19            put x=date9.;

20            run;

x=10JAN2011

Contributor
Posts: 39

Re: Character to SAS Date conversion

Posted in reply to data_null__

Thanks for the reply.

So I have modified the code to:

data want;

set have;

dt= put(datepart(Date),5.);

format dt date9.;

run;

But the error exits. The log says:

"Note 484-185: Format $Date was not found or could not be loaded".

Solution
‎02-01-2013 11:00 AM
Super Contributor
Posts: 334

Re: Character to SAS Date conversion

Not sure its clear what you are trying to do.

If all you need is the date to show as 10Jan2011 then you do not need to use the put function (just the datepart) and just associate the format with the variable. If you are converting to a character variable then I think you need to just replace the 5. format in the put with the date9. format.

EJ

EDIT:

Sample code to illustrate

data temp;

  x = 18637;

  format x date9.;

  x2= put(x,date9.);

  y='10Jan2011 12:00:00'dt;

  y2 = datepart(y);

  format y2 date9.;

  y3 = put(y2,date9.);

run;

Contributor
Posts: 39

Re: Character to SAS Date conversion

Thank you, esjackso1. That was right, the code is working fine.

Super Contributor
Posts: 578

Re: Character to SAS Date conversion

The DTDATE9. format display DDMMMYYYY when applied to a datetime variable.

Contributor
Posts: 39

Re: Character to SAS Date conversion

Thank you, DBailey. Your answer works just fine.

Respected Advisor
Posts: 3,799

Re: Character to SAS Date conversion

The date I have is 10JAN2011. I have converted to numeric and to character and I get the value 18637 in character format.

Do NOT convert to character.  When you get the NUMBER 18637 you are done.  All you need after that is to associate a date format.

Message was edited by: data _null_

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 2763 views
  • 6 likes
  • 4 in conversation