DATA Step, Macro, Functions and more

date formats

Reply
Contributor
Posts: 63

date formats

Hi I have following data ,

 

dates

13APR1999

120499

2016-11-19

11-18-2016

2016

un-18-2018

un-unk-2016

 

I need to get all these dated to yymmdd10. format,

 

 

Contributor
Posts: 42

Re: date formats

what should be the output for 

2016

un-18-2018

un-unk-2016

?

Contributor
Posts: 63

Re: date formats

for 2106 the output should be 2016

for 

un-18-2018      2016-18-  

un-unk-2016     2016-  -   

there should be blank in the place of un and unk

Trusted Advisor
Posts: 1,406

Re: date formats

If do not intend to convert the input into sas date variable then just read it a 

alphanumeric and replace the un/unk inro space:

 

data want;

infile datalines truncover;

input datex $10.;

  datex = tranword(datex, '  ','un');

  datex = translate(datex,' ','k');

datalines;

13APR1999

120499

2016-11-19

11-18-2016

2016

un-18-2018

un-unk-2016

; run;

Contributor
Posts: 63

Re: date formats

I need to convert that to sas date varaible and display in yymmdd10.format

Trusted Advisor
Posts: 1,406

Re: date formats

how would you interpre input like: 09-05-2015

is it a ddmmyy10. informat or mmddyy10. informat ?

 

and what about: 120499 - is ir 12apr1999 or 12apr2099 or 04dec1999 etc?

 

You cannot have a unque result unless you have some more restrictions.

Contributor
Posts: 63

Re: date formats

for all the possible sas date informats i need to convert to yymmdd10. format,there I have given only few date informats,

 

Contributor
Posts: 42

Re: date formats

You can not have this kind of output in the column with ordinary yymmdd10. format.

 

Either construct your own format

 

or

 

extract Year Month and Date from your input and based on availability of these values construct date column formatted as yymmdd10.

Contributor
Posts: 63

Re: date formats

ok..

 

In the case of 13APR1999 input, can I compare the variable with sas datesets and convert them to yymmdd10. format, like

 

if datetest=date9. then 

d1=input(datetest,yymmdd10.)

 

In these way can i compare the varable with every sas date format and convert them to yyddmm10. format

Super User
Posts: 6,972

Re: date formats


molla wrote:

for 2106 the output should be 2016

for 

un-18-2018      2016-18-  

un-unk-2016     2016-  -   

there should be blank in the place of un and unk


You can`t do that with the standard YYMMDD10. format; it needs a date value as input, and will always display days and months.

You will have to keep year, month and day in different variables so you can store the un/unk as missing values there, while preserving the non-missing year (or month).

BTW, there is no 18th month in the Gregorian calendar Smiley Wink

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 63

Re: date formats

My requirement is consider a varaible which contain all the possible date informats of sas ,some of the months ,years are missing in the observations ,I need to convert all those to a common standard frmat yymdd10.

Contributor
Posts: 63

Re: date formats

here 18 is not month it is date, it id in mmddyy10. format 

Super User
Posts: 17,963

Re: date formats

See the notes and solution in this paper

 

http://support.sas.com/resources/papers/proceedings11/117-2011.pdf

Super User
Posts: 6,972

Re: date formats


molla wrote:

My requirement is consider a varaible which contain all the possible date informats of sas ,some of the months ,years are missing in the observations ,I need to convert all those to a common standard frmat yymdd10.


This is only possible if you keep year, month and day in separate variables. A SAS date value (just like a date value in any programming environment) is a count of days from a certain offset (1960-01-01 in the case of SAS) and will always contain a valid month and day.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,963

Re: date formats

Do you know the informat ahead of time? 

Or are you deriving it based on the appearance of the data?

 

As mentioned, mmddyy and ddmmyy are both valid informats that cannot be uniquely identified. 

 

If you don't know the format this may not be possible. 

The SAS catch all informat is ANYDTDTE. 

 

For your exact data, please post the expected results. 

Ask a Question
Discussion stats
  • 15 replies
  • 403 views
  • 1 like
  • 5 in conversation