DATA Step, Macro, Functions and more

Date conversion to Character

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Date conversion to Character

I have a date in my data named actual_dlvry_date. It looks like this:

 

05/21/2016

 

ad_dt_char=PUT(datepart(actual_dlvry_date), mmddyy10.); * Convert to a char value ;

I'm attempting to make a character version of the date using this code, but my date value comes out as:

 

01/01/1960

 

Any suggestions for what I may be doing wrong here?


Accepted Solutions
Solution
‎09-23-2016 02:34 PM
Super User
Posts: 11,343

Re: Date conversion to Character

Posted in reply to buechler66

Remove the DATEPART.

 

DATEPART assumes the variable is a datetime which is a count of seconds at heart. Date values are counts of days. So since there are many fewer days when treated as seconds the date portion is often going to correspond to 0 or the base SAS date of 1/1/1960.

View solution in original post


All Replies
Solution
‎09-23-2016 02:34 PM
Super User
Posts: 11,343

Re: Date conversion to Character

Posted in reply to buechler66

Remove the DATEPART.

 

DATEPART assumes the variable is a datetime which is a count of seconds at heart. Date values are counts of days. So since there are many fewer days when treated as seconds the date portion is often going to correspond to 0 or the base SAS date of 1/1/1960.

Regular Contributor
Posts: 212

Re: Date conversion to Character

Thanks so much. That did the trick. I appreciate your help.
Frequent Contributor
Posts: 123

Re: Date conversion to Character

Posted in reply to buechler66

The datepart function is used to extract the date from a datetime value. Your date is not a datetime value, datetime values look like 01feb94:8:45. To convert your number date to character, you simply need a put statement with no datepart. Like this:

 

ad_dt_char=PUT(actual_dlvry_date, mmddyy10.); * Convert to a char value ; 

 

Does this answer your question?

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 301 views
  • 2 likes
  • 3 in conversation