DATA Step, Macro, Functions and more

How to define date from a given date

Reply
Occasional Contributor
Posts: 7

How to define date from a given date

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 

Super User
Super User
Posts: 9,227

Re: How to define date from a given date

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.

Frequent Contributor
Posts: 79

Re: How to define date from a given date

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
PROC Star
Posts: 229

Re: How to define date from a given date

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.

Super User
Super User
Posts: 9,227

Re: How to define date from a given date

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?

Occasional Contributor
Posts: 7

Re: How to define date from a given date

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

 

 

 

Super User
Super User
Posts: 9,227

Re: How to define date from a given date

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. 

Occasional Contributor
Posts: 7

Re: How to define date from a given date

OK , thanks for reply

Super User
Posts: 9,611

Re: How to define date from a given date

Second what @RW9 said. Return to sender and ask what they've been smoking.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 8 replies
  • 176 views
  • 5 likes
  • 5 in conversation