SAS Office Analytics, SAS Add-In for Microsoft Office, and other integrations

SAS function equivalent to EXCEL function iferror()

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

SAS function equivalent to EXCEL function iferror()

Could someone tell me an equivalent SAS function for the EXCEL function iferror(), which I could use in a PROC SQL variable assignment.

For example, in an datastep if I have a SAS variable defined as:

AGE = YRDIF( birthdate, today(), 'ACTUAL');

but for some reason in my data the birthdate is missing, then I get an error. This does not stop SAS, but I would like it to put, for example, 22 (the average age) if the birthdate is missing. In a datastep I would write:

if missing(birthdate) then AGE = YRDIF( birthdate, today(), 'ACTUAL'); else AGE = 22;

However, in a PROC SQL, how would I do this? All I can think of is if there were an equivalent SAS function like the EXCEL iferror() function, then I could do this in one line. In other words, I would like to write the "equivalent" of (using the EXCEL function):

AGE = iferror( YRDIF( birthdate, today(), 'ACTUAL'), 22);

If the first argument is null or error, then the second argument is returned for AGE.

Thanks in advance.

Ian.


Accepted Solutions
Solution
‎11-24-2014 04:04 AM
Esteemed Advisor
Esteemed Advisor
Posts: 6,726

Re: SAS function equivalent to EXCEL function iferror()

Here is a one line approach:

data have;

  birthday='01jan2012'd;

  output;

  birthday=.;

  output;

run;

proc sql;

  create table WANT as

  select  *,

          yrdif(COALESCE(BIRTHDAY,'15JUL1992'd),today(),'actual') as AGE

  from    HAVE;

quit;

So coalesce takes the first non-null value from the list, birthday first, then your default, and caluculates on that value.

View solution in original post


All Replies
Super Contributor
Posts: 334

Re: SAS function equivalent to EXCEL function iferror()

I would suggest you try the equivalent to Excel's "isblank" instead of "iserror". Could look like:

Data A;
  Input BirthdateSmiley Very Happyate9.; Format Birthdate Date9.;
  Datalines;
  01JAN2000
  09SEP2006
  .
  ;
Run;

Proc SQL;
  Create Table B As
  Select *,
         Case When Missing (Birthdate) Then 22 Else
                   YrDif(Birthdate,Today(),'Actual') End As Age
  From A;
Quit;

Contributor
Posts: 53

Re: SAS function equivalent to EXCEL function iferror()

Thanks very much. I just happened to find almost exactly the same solution after searching some more. Thanks.

Ian.

Solution
‎11-24-2014 04:04 AM
Esteemed Advisor
Esteemed Advisor
Posts: 6,726

Re: SAS function equivalent to EXCEL function iferror()

Here is a one line approach:

data have;

  birthday='01jan2012'd;

  output;

  birthday=.;

  output;

run;

proc sql;

  create table WANT as

  select  *,

          yrdif(COALESCE(BIRTHDAY,'15JUL1992'd),today(),'actual') as AGE

  from    HAVE;

quit;

So coalesce takes the first non-null value from the list, birthday first, then your default, and caluculates on that value.

Contributor
Posts: 53

Re: SAS function equivalent to EXCEL function iferror()

thanks a lot!

Post a Question
Discussion Stats
  • 4 replies
  • 2864 views
  • 4 likes
  • 3 in conversation