DATA Step, Macro, Functions and more

formatting from number to date

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

formatting from number to date

Hello 

I'm working on a data which has numeric variable in the following way.

 

20061213
20061213
20061213
20061214

 

I tried converting it in to date format by using following code.

 

data TEST;

set recnou.rec19sep17;

format dtmep date8. ;

where CDFPRO='P';

keep nocont cnp dtmep unpaid;

run;

 

I'm getting following output.

 

********
********
********
********
********
********
********
********
********

 

can any one please tell me the reason for the above output and how do I rectify the problem.

i want the date to be mm/dd/yyyy

 

Thank you


Accepted Solutions
Solution
‎09-21-2017 09:30 AM
Trusted Advisor
Posts: 1,931

Re: formatting from number to date

[ Edited ]

You can't format it as a date because for that to work, the number has to be a legitimate SAS date value, in other words, it must be the number of days since Jan 1, 1960. Try this UNTESTED CODE

 

newvar=input(put(dtmep,$8.),yymmdd8.);
format newvar date8.;

View solution in original post


All Replies
Super User
Posts: 5,437

Re: formatting from number to date

If you have readable dates as numeric without a format, you need to transform the values.
First use put () to a text string, ind use input() to do the conversion using a specific informat that matches your current date format.
Data never sleeps
New Contributor
Posts: 2

Re: formatting from number to date

i solve it  right this

data TEST;

set recnou.rec19sep17;

DATE2 = INPUT(PUT(Dtmep,8.),yyMMDD8.);

FORMAT DATE2 MMDDYY10.;

where CDFPRO='P';

keep nocont cnp date2 unpaid;

run;

Solution
‎09-21-2017 09:30 AM
Trusted Advisor
Posts: 1,931

Re: formatting from number to date

[ Edited ]

You can't format it as a date because for that to work, the number has to be a legitimate SAS date value, in other words, it must be the number of days since Jan 1, 1960. Try this UNTESTED CODE

 

newvar=input(put(dtmep,$8.),yymmdd8.);
format newvar date8.;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 83 views
  • 0 likes
  • 3 in conversation