DATA Step, Macro, Functions and more

formatting numeric to date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

formatting numeric to date

Hi,

 

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

 

01.2015

04.2015

06.2015

09.2015

 

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

 

format variable yymmn6.;

 

I'm getting following output.

 

196001

196001

196001

196001

 

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

 

Thanks alot in advance.

 


Accepted Solutions
Solution
‎02-10-2017 08:57 AM
Super User
Super User
Posts: 7,392

Re: formatting numeric to date

I don't know what a VDI is, but do bear in mind for future posts that this information helps us understand things at your end which we can't see.

 

No, SAS dates have to be complete dates with Day Month and Year as they are stored as a number of days from a set date.  The format applied to this number can show the data slightly differently, but cannot alter the underlying data.

Ie 

01JAN2015 = 20089 days <the number 20089 is what is stored in the variable.

 

If you just want to display first part after second, then a cat and scan can do it (though always recommended to store dates as numeric dates, it just makes life easier):

data want;
  my="01.2015";
  ym=cats(scan(my,2,"."),scan(my,1,"."));
run;

That is string manipulation.

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: formatting numeric to date

Hi,

 

To get a good answer it is recommended to post test data, in the form of a datastep (so we don't guess what you have), post output you want, and show code/logs of what does not work.

I am guesssing 01.2015 is a character variable and means Jan 2015?  If so then you need to input it as a date, give it a default say 01, and then format it to what you want (SAS dates are all numeric, number of days since a certain timepoint):

data want;
  my="01.2015";
  full_date=input(cats('01.',my),ddmmyy10.);
  format full_date yymmn6.;
run;
Occasional Contributor
Posts: 12

Re: formatting numeric to date

Hi RW9,

 

I'm working in a VDI, so couldn't post the data and log information.

 

Thanks for your reply.

 

I did the same and it worked.

 

 

Is there a way to do it with out concatenating 01?

 

Thanks.

Solution
‎02-10-2017 08:57 AM
Super User
Super User
Posts: 7,392

Re: formatting numeric to date

I don't know what a VDI is, but do bear in mind for future posts that this information helps us understand things at your end which we can't see.

 

No, SAS dates have to be complete dates with Day Month and Year as they are stored as a number of days from a set date.  The format applied to this number can show the data slightly differently, but cannot alter the underlying data.

Ie 

01JAN2015 = 20089 days <the number 20089 is what is stored in the variable.

 

If you just want to display first part after second, then a cat and scan can do it (though always recommended to store dates as numeric dates, it just makes life easier):

data want;
  my="01.2015";
  ym=cats(scan(my,2,"."),scan(my,1,"."));
run;

That is string manipulation.

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 120 views
  • 0 likes
  • 2 in conversation