DATA Step, Macro, Functions and more

IFN function for date format?

Reply
Frequent Contributor
Posts: 101

IFN function for date format?

In my source file, i have 2 columns For Eg:

Name    DOJ
Ram     13/12/1988

Kumar   21/05/1989

Ishant    110488

Can we use IFN Function here?

IFN(DOJ = 'DATE FORMAT',DOJ,default date)

whether it will work?

I tried but no fruitful result.

Help me on this.

Super Contributor
Posts: 282

Re: IFN function for date format?

Posted in reply to Helannivas

Hi,

What value have you tried for 'DATE FORMAT'? The equality test will just try to match the values on either side of the equal sign.

What error or warning message are you getting for the value you have tried?

Are you just trying to make sure the value of DOJ conforms to a certain format or is equal to a certain value?

Regards,

Amir.

Frequent Contributor
Posts: 101

Re: IFN function for date format?

Hi,

I dont know what to give in that 'DATE FORMAT'.

I need to check the format(need to be in dd/mm/yyyy).

For Eg: If DOJ is 13/12/1988 means o/p will be same

            If DOJ is 13121988 means o/p will be any default date.

Its possible by using IFN function?

If not,how to proceed further.

Occasional Contributor
Posts: 18

Re: IFN function for date format?

Posted in reply to Helannivas

Hi Helannivas,

Name    DOJ
Ram     13/12/1988

Kumar   21/05/1989

Ishant    110488

In your input dataset Name and Doj both the variables are character please correct me if my understanding is wrong.

Occasional Contributor
Posts: 18

Re: IFN function for date format?

Posted in reply to Helannivas

Hi Helannivas,

The following code will solve your problem if the data type of DOJ is Character in your input dataset.

data out_d;

set in_d;

pos=findc(doj,'/',1);

if pos = 0 then  new_d = mdy(month(substr(doj,3,2)),day(substr(doj,1,2)),year(substr(doj,5,2)));

else new_d = mdy(month(substr(doj,4,2)),day(substr(doj,1,2)),year(substr(doj,7,4)));

format new_d ddmmyy8.;

run;

Please give your feedback.

Thank you.

Gaurang Patel

Super Contributor
Posts: 282

Re: IFN function for date format?

Posted in reply to Helannivas

Hi,

In your code you show:

IFN(DOJ = 'DATE FORMAT',DOJ,default date)

The ifn function returns a numeric expression, so you can only return DOJ if it is numeric; ifc might be an alternative.

You have shown that some values of DOJ have '/' and some do not which would imply it is character.

Is DOJ supposed to be a numeric or a character variable?

SAS holds date values as numerics and a lot of SAS functions expect dates as numerics.

Regards,

Amir.

Super Contributor
Posts: 282

Re: IFN function for date format?

Posted in reply to Helannivas

Hi,

If DOJ is a character variable then you could try something like:

data want;

  set have;

  retain default '01/01/2000';

  doj2=ifc(prxmatch('/\d\d\/\d\d\/\d{4}/',doj),doj,default);

run;

Regards,

Amir.

Message was edited by: Amir Malik - used retain instead of assignment.

Regular Contributor
Posts: 151

Re: IFN function for date format?

Posted in reply to Helannivas

The fact that your data is in different formats indicates that the variable is character.  In which case you can use a PERL regular expression to check the format you're looking for (you could expand on my example below to ensure the numbers entered are valid for dates).  If you just wanted to convert these values to dates then you can use the ANYDTDTE. informat which will convert all the examples you provided.

data have;

input name $ doj $ :10.;

datewant1=input(doj,anydtdte10.);

datewant2=ifn(prxmatch('/\d{2}\/\d{2}\/\d{4}/',doj)=0,today(),input(doj,anydtdte10.));

format datewant: date9.;

datalines;

Ram     13/12/1988

Kumar   21/05/1989

Ishant    110488

;

run;

Valued Guide
Posts: 765

Re: IFN function for date format?

Posted in reply to Helannivas

Hi.  If the dates are always day-month-year, you can use a DDMMYY. informat since it works both with and without intervening slashes .

data x;

input name : $10. xyz : $10.;

datalines;

Ram 13/12/1988

Kumar 21/05/1989

Ishant 110488

Armondo 31122012

Steve  28/02/10

;

data x;

format doj ddmmyy10.;

set x;

doj = input(xyz,ddmmyy10.);

run;

       doj    name      xyz

13/12/1988    Ram       13/12/1988

21/05/1989    Kumar     21/05/1989

11/04/1988    Ishant    110488

31/12/2012    Armondo    31122012

28/02/2010    Steve      28/02/10

Super Contributor
Posts: 1,636

Re: IFN function for date format?

Hi Mike,

Welcome back!!! Hope to see you soonSmiley Happy!

Ask a Question
Discussion stats
  • 9 replies
  • 646 views
  • 1 like
  • 6 in conversation