BookmarkSubscribeRSS Feed
Helannivas
Quartz | Level 8

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.

9 REPLIES 9
Amir
PROC Star

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.

Helannivas
Quartz | Level 8

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.

Gaurang_sas
Fluorite | Level 6

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.

Gaurang_sas
Fluorite | Level 6

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

Amir
PROC Star

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.

Amir
PROC Star

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.

Keith
Obsidian | Level 7

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;

MikeZdeb
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

Hi Mike,

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

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
  • 9 replies
  • 2235 views
  • 1 like
  • 6 in conversation