BookmarkSubscribeRSS Feed
harry_87
Calcite | Level 5

Hi ,

       Need help

I have been provided with various dates in .txt file

 

For example

2018/04/22

20140422

2018-04-22

2018-04-22 05:34:46

04222018

 

I want to define date format pattern, and need output as

yyyy/mm/dd

yyyymmdd

yyyy-mm-dd

yyyy-mm-dd hh:mm:ss

mmddyyyy

 

and also Like to know, if date id above for example 

13022018

if it is above 12 as you see above starting 2 digit is 13 so it is dd only not month because month will be 01-12 right,

so I dont no, how to handle this type of dates.

Could you please help me with this 

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

What you will need to do is some string manipulation.  You can look into pearl regular expressions:

https://support.sas.com/rnd/base/datastep/perl_regexp/regexp-tip-sheet.pdf

And build up patterns from there.

 

Or you go through and build up your conditions using if logic, e.g.:

If string contains / then delimter=/

If string contains - then delimter=-

If length > 10 then date/time

...

 

I am not writing it all out for you.

 

As for: "and also Like to know, if date id above for example 

13022018

if it is above 12 as you see above starting 2 digit is 13 so it is dd only not month because month will be 01-12 right,

so I dont no, how to handle this type of dates." - I have no idea what you mean.

mnjtrana
Pyrite | Level 9

Hi,

 

You can use the anydtdte21. informat to read this type of data, it reads and extracts the date value from various date, time, and datetime forms.

 

Pls refer to code below.

 


data need;
input date_var anydtdte21.;
datalines;
2018/04/22
20180422
2018-04-22
2018-04-22 05:36:02
13042018
run;

More details here:

 

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002605538.htm

 

Please give a like and mark as solution if it helped.


Cheers from India!

Manjeet
s_lassen
Meteorite | Level 14

You could use the ANYDTDTE. informat:

data want;

  informat date anydtdte.;

  format date date9.;

  input date;

cards;

2018/04/22

20140422

2018-04-22

2018-04-22 05:34:46

04222018

;run;

In some cases, it is not clear how a date whould be read. Does 010403 mean 01APR2003, 04JAN2003 or 03APR2001? You can set your preferred input style with the DATESTYLE option.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Perhaps be clearer about what you want.  Do you want, as I understood from your post, to get the structure of the date as a text pattern, or do you just want to read in some text into a date format variable as @s_lassen has presented?

harry_87
Calcite | Level 5

I want structure as date as date pattern 

But Yes, if 01122018 so how to handle this 01Dec2018 or 21JAN2018 that I dont no, 

options datestyle will work not sure

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

In which case then use perl regular expressions or if statements as I said to process the string.

 

AS for your second question, we cannot answer this.  Only the person who created that date can tell you what the intent was.  There is no logical way of identifying which of the possible dates that is supposed to represent.  To be honest if it was me, I would be sending that data back to source and asking them to do their job properly by creating useful accurate data, and provide a data description to boot. 

harry_87
Calcite | Level 5

OK , thanks for reply

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1089 views
  • 5 likes
  • 5 in conversation