DATA Step, Macro, Functions and more

Convert date format in a character variable

Reply
Super Contributor
Posts: 272

Convert date format in a character variable

[ Edited ]

Hi I have a question in my code;

 

I have date values in character format;

 

Exp:  Date   2015-09-25

 

I need to convert the date into 2015/09/25 which is in character format.

 

My code:

date1=input(date,yymmdd10.);
format date yymmdds10.;

 

But the code resulted in value numeric format. I need the value in chracter format. 2015/09/25. Please help.

 

Thank you

Respected Advisor
Posts: 4,927

Re: data step

Posted in reply to knveraraju91

date = translate(date,"/","-");

PG
Super User
Posts: 5,431

Re: data step

Posted in reply to knveraraju91
Why do need it in char? Storing dates as numerical is much more flexible. There are tons of date functions available to you, and you could change the date format at blink of a second - no ned to rewrite to do conversion.
Data never sleeps
Super User
Super User
Posts: 7,975

Re: data step

There is actually two instances where you might want character dates - ISO dates for instance are character.  The other is for display purposes, most times put to format is fine, but sometimes you may want additional information for report purposes.  

But I totally agree with you, unless there is a very good reason there is no reason to not use numeric date or time variables as they are far easier to use.  I suspect he is trying to put these in macro list or something similar (in which case don't).

Super Contributor
Posts: 441

Re: Convert date format in a character variable

Posted in reply to knveraraju91

You can use the inverse of INPUT, PUT:

 

date2=put(date1, yymmdds10.);

I refrain from disputing your expressed need for this. I think it is out of order until you have been asked questions on the matter, which has not happened. And I won't.

 

Hope this helps,

-Jan.

Super Contributor
Posts: 272

Re: Convert date format in a character variable

Posted in reply to jklaverstijn

Hi all,,

 

Thank you very much. Both codes are working for me as I am validating a dataset using proc compare. Thank yoiu.

Ask a Question
Discussion stats
  • 5 replies
  • 323 views
  • 2 likes
  • 5 in conversation