BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vnreddy
Quartz | Level 8

Hi,

 

I have a date format something like YYYYMMDD this: 20200329

How can i convert this to DDMMYYYY : 29032020

I want the column to be in numeric format with DDMMYYn8. when i use this format i am getting Date format column with *** as values

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@vnreddy wrote:

Hi @Kurt_Bremser 

 

Variable is in Numeric format.

No format is attached to it, i want the output column as well to be in Numeric format.


In SAS a format is instructions for how to display the values.  You are using the word FORMAT when you mean TYPE.  SAS has two types of variables, fixed length strings and floating point numbers.

 

You want to convert a number to another number. You want to treat the original numbers as if they are in the form YY,YYM,MDD (so the tens and ones place are the day of the month) and treat the target numbers as if they are in the form DD,MMY,YYY (so the tens are ones place are the year within the century).

 

So use PUT + Z8. format to convert the original number to text. Then use INPUT() + YYMMDD. informat to convert that text into a date number.  Then you can use PUT() + DDMMYYN format to convert that date into a string in the right style. Now you can use an INPUT() function with the normal numeric informat to convert that string into a number.

want = input(put(input(put(have,z8.),yymmdd8.),ddmmyyn8.),8.);

View solution in original post

17 REPLIES 17
PeterClemmensen
Tourmaline | Level 20

Simply use another format? ddmmyy10.?

Jagadishkatam
Amethyst | Level 16
data have;
date=input('20200301',yymmdd8.);
format date ddmmyyn8.;
run;
Thanks,
Jag
vnreddy
Quartz | Level 8

Hi @Kurt_Bremser 

 

Variable is in Numeric format.

No format is attached to it, i want the output column as well to be in Numeric format.

Kurt_Bremser
Super User

Then you can convert it "in place":

data want;
set have;
date = input(put(date,z8.),yymmdd8.);
format date ddmmyyn8.;
run;

@vnreddy wrote:

Hi @Kurt_Bremser 

 

Variable is in Numeric format.

No format is attached to it, i want the output column as well to be in Numeric format.


 

vnreddy
Quartz | Level 8

Hi @Kurt_Bremser 

 

Thank you for solution.

I does gave me right output, but my expected output format should be Numeric instead of Date format.

 

Kurt_Bremser
Super User

@vnreddy wrote:

Hi @Kurt_Bremser 

 

Thank you for solution.

I does gave me right output, but my expected output format should be Numeric instead of Date format.

 


That would be one of the dumbest things you can do. Keep dates as SAS dates, and use the proper format when outputting, either to a report or for use in another system that expects a string with a certain date format.

vnreddy
Quartz | Level 8

@Kurt_Bremser i know, unfortunately there is a new date source which came in few days back.

Need to map this column with existing column, which is in Numeric format.

Existing column from old system which is in Numeric format was used in number of DI jobs. 

Rather than changing the old column format, it would be a good idea to change the new column to an existing format for rest of DI jobs to work.

Kurt_Bremser
Super User

Keeping dates like that is, well, good manners prevent me from writing what comes to my mind. I mean that. Really.

Take the opportunity to clean this NOW. Any day this is allowed to exist is a day wasted.

You already have a load of jobs that need to be cleaned one day. Adding another to this is only a loan you take out on your future, and it WILL come back to bite you in your behind. IT WILL.

 

DavePrinsloo
Pyrite | Level 9
Just to expand to convert back to numeric:
data want;
set have;
date = input( input(put(date,z8.),yymmdd8.), best.);
format date 8.;
run;
Kurt_Bremser
Super User

@DavePrinsloo wrote:
Just to expand to convert back to numeric:
data want;
set have;
date = input( input(put(date,z8.),yymmdd8.), best.);
format date 8.;
run;

I have a t-shirt from the schlockmercenary.com store. Its front reads:

"If it's stupid and it works, it's still stupid, and you're lucky."

 

That just came to my mind during this discussion. Storing dates as some kind of non-date deprives everybody involved from making use of all the features SAS has built to handle date values.

 

Insanity.

 

While the conversion is, as you posted, a very simple thing, IT MUST NOT BE DONE. Dates are dates are dates.

ballardw
Super User

As just one example of why the "numeric that sort of imitates a date" is a poor idea create a graph with the numeric but not a date values as a horizontal axis. Either a series or scatter would likely be best that has daily values. Make sure that the "dates" involved cross a year boundary.

 

If the graph doesn't look odd then I want to see the data.

 

 

vnreddy
Quartz | Level 8

Hi,

 

I have a date value like 20200321 like column A below

I want the output column in this format 21032020 like column B below

A B
20200321 21032020

 

Source column was in Numeric format, and i want the target column as well to be in Numeric format.

PeterClemmensen
Tourmaline | Level 20

Like this?

 

data test;
    a = '21mar2020'd;
    b = a;

    format a yymmddn8. b ddmmyyn8.;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 17 replies
  • 2629 views
  • 3 likes
  • 7 in conversation