BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jmmedina252
Calcite | Level 5

I have a string that is mmddyy (example:043023) that I'm having trouble converting to date.  I have tried these functions and they have not worked:

 

old_date

040623

033023

040623

 

new_date = input(old_date,MMDDYY6.);

new_date = input(old_date,MMDDYYN6.);

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Your first option appears to work for me.

 

data have;
input old_date $;
new_date = input(old_date, mmddyy6.);
format new_date date9.;
cards;
040623
033023
040623
;
run;

proc print;run;

Reeza_0-1682363577957.png


@jmmedina252 wrote:

I have a string that is mmddyy (example:043023) that I'm having trouble converting to date.  I have tried these functions and they have not worked:

 

old_date

040623

033023

040623

 

new_date = input(old_date,MMDDYY6.);

new_date = input(old_date,MMDDYYN6.);


 

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

What do you mean by "they have not worked"?  Are you getting log messages with notes or error messages to that effect?  Is the log quiet, but the resulting value is not as expected? if so, what did you get that was unexpected?.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

Your first option appears to work for me.

 

data have;
input old_date $;
new_date = input(old_date, mmddyy6.);
format new_date date9.;
cards;
040623
033023
040623
;
run;

proc print;run;

Reeza_0-1682363577957.png


@jmmedina252 wrote:

I have a string that is mmddyy (example:043023) that I'm having trouble converting to date.  I have tried these functions and they have not worked:

 

old_date

040623

033023

040623

 

new_date = input(old_date,MMDDYY6.);

new_date = input(old_date,MMDDYYN6.);


 

Kurt_Bremser
Super User

Is old-date really of type character, or a number without a date format?

If character, remove leading blanks:

new_date = input(left(old_date),MMDDYY6.);

And do not forget to assign a date format:

format new_date yymmdd10.;

If you still have trouble, please post the complete (all code and messages) log of your conversion step.

PaigeMiller
Diamond | Level 26

There are several things we would need to know in order to give you a definitive answer.

 

One thing is whether this variable called OLD_DATE is numeric or character. Please let us know. Another thing we would need to know is the appearance of the variable (you have shown us that) and the appearance unformatted (please show us that).

 

Given all these things, we can then give you a definitive answer. Without the above information, we're guessing.

 

Also, there is no such informat as MMDDYYN6. There is an informat MMDDYY6. which will work only on character variables, one reason why we need to know if OLD_DATE is numeric or character. Finally, do not show us partial code. Be generous, if you can't get a DATA step to work, show us the entire DATA step and the entire LOG for that data step.

--
Paige Miller
Tom
Super User Tom
Super User

If OLD_DATE is character (and does not have leading spaces) then you can convert it with the MMDDYY6 informat and get a pretty good estimate of the date (depending on whether your dates might be more than 80 years old or not).

data have;
  input old_date $6. ;
cards;
040623
033023
040623
;
data want;
  set have;
  new_date = input(old_date,mmddyy6.);
  format new_date yymmdd10.;
run;

Results:

OBS    old_date      new_date

 1      040623     2023-04-06
 2      033023     2023-03-30
 3      040623     2023-04-06

If OLD_DATE is a numeric variable that is using the Z6. format to display so that the leading zeros appear then you will first need to convert it to a character string before using the INPUT() function.

data have;
  input old_date ;
  format old_date z6.;
cards;
040623
033023
040623
;
data want;
  set have;
  new_date = input(put(old_date,z6.),mmddyy6.);
  format new_date yymmdd10.;
run;

Results

OBS    old_date      new_date

 1      040623     2023-04-06
 2      033023     2023-03-30
 3      040623     2023-04-06

And if OLD_DATE is already a DATE value, but is using the MMDDYY6. format to display only six digits then you don't need to change the value at all.  You can just display the values using a different format.

data have;
  input old_date mmddyy6.;
  format old_date mmddyy6.;
cards;
040623
033023
040623
;
data want;
  set have;
  new_date = old_date;
  format new_date yymmdd10.;
run;

Results

OBS    old_date      new_date

 1      040623     2023-04-06
 2      033023     2023-03-30
 3      040623     2023-04-06

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 651 views
  • 1 like
  • 6 in conversation