BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi,

I need to convert the texted date variable to a particular format.

Eg:

data have;
input R_Date$20.;
cards;
31/01/2000
15/3/2015
30-5-2010
30-12-2018
31/7/20
15/2/12
18/05/15
11-08-08
2000
1998
;

Expected result

01/31/2000
03/15/2015
05/30/2010
12/30/2018
07/31/2020
02/15/2012
05/18/2015
08/11/2008
01/01/2000
01/01/1998

 

Kindly suggest a code to get MM/DD/YYYY format.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

EDIT: i removed my "solution" after reading the fully valid comment by @Kurt_Bremser 

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

This is not data, this is unusable crap.

11-08-08

could be

2011-08-08

2008-08-11

2008-11-08

not even the year can be determined with any confidence. So there simply is no way to reliably make dates out of this. And reliable processes are what we need and strive for. To roll dice, I don't need a data warehouse.

Anybody who still uses 2-digit years after Y2K is * ***** ***** in my eyes, and needs to be cared for in a proper institution for the mentally challenged.

 

Print that "data" on a sheet, wrap it around a baseball bat, and go to the person that sent you this. Apply the baseball bat liberally. Maybe some neurons get into contact and start to fire.

 

Sorry for my rant, but things like this severely damage my belief in a long-term future for the human species.

 

PS anybody who finds exaggeration or irony can keep it and use it freely.

andreas_lds
Jade | Level 19

EDIT: i removed my "solution" after reading the fully valid comment by @Kurt_Bremser 

Tom
Super User Tom
Super User

So are you assuming the values should be in DMY order?  If so use the DDMMYY informat.  If you want to get the year only values you will need to add more logic to handle those cases.

I wouldn't compound the confusing by using either DMY or MDY order for displaying the derived dates. Use an unambiguous format like YYMMDD10. or DATE9. .

data want;
  set have ;
  date=coalesce(input(r_date,??ddmmyy10.),input(r_date,??anydtdte20.));
  if missing(date) and length(r_date)=4 and not missing(input(r_date,??4.))
    then date=mdy(1,1,input(r_date,4.))
  ;
  format date yymmdd10.;
run;

proc print;
run;
Obs    R_Date              date

  1    31/01/2000    2000-01-31
  2    15/3/2015     2015-03-15
  3    30-5-2010     2010-05-30
  4    30-12-2018    2018-12-30
  5    31/7/20       2020-07-31
  6    15/2/12       2012-02-15
  7    18/05/15      2015-05-18
  8    11-08-08      2008-08-11
  9    2000          2000-01-01
 10    1998          1998-01-01

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1073 views
  • 2 likes
  • 4 in conversation