DATA Step, Macro, Functions and more

Convert date format in a character variable

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:

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,649

Re: data step

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

Super User
Posts: 5,257

Re: data step

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,401

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: 408

Re: Convert date format in a character variable

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,


Super Contributor
Posts: 272

Re: Convert date format in a character variable

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
  • 5 in conversation