## is ther a logical function similar to "iif" that can be used into a Data step ?

Solved
Occasional Contributor
Posts: 5

# is ther a logical function similar to "iif" that can be used into a Data step ?

Happy new year everybody,

I'm looking for such a function. I already noticed something like this in the MDX / OLAP language tools. WHat about traditional data steps? e.g.:

data test;

input     sexe @@;

MF     =<function>(sexe=2, "Female", "Male");

cards;

1 2 0 99

;

Thank you.

Michel, Montpellier (France)

Accepted Solutions
Solution
‎01-04-2013 07:49 AM
Posts: 1,147

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

hi Michel,

i hope you can use the put function. and below is the code i have written.

proc format;

value gen 1,0,99='Male'

2='Female';

run;

data test;

input     sexe @@;

MF     =put(sexe,gen.);

cards;

1 2 0 99

;

Thanks,

Jag

Thanks,
Jag

All Replies
Posts: 1,147

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

Hi Lehmann,

i am a bit confused, could you please elaborate your question and the output which you would like to get. This will help me to try, give an answer.

Thanks,

Jag

Thanks,
Jag
Occasional Contributor
Posts: 5

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

My problem is described in the object field of my first message. It's very simple: how to keep on being lazy?

by writing something like          MF     =<function>(sexe=2, "Female", "Male");

instead of                               if sexe=2 then MF="Female"; else sexe="Male";

(I also use formats or informats for some purpose, but that is not the point today).

Yours,
Michel

Solution
‎01-04-2013 07:49 AM
Posts: 1,147

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

hi Michel,

i hope you can use the put function. and below is the code i have written.

proc format;

value gen 1,0,99='Male'

2='Female';

run;

data test;

input     sexe @@;

MF     =put(sexe,gen.);

cards;

1 2 0 99

;

Thanks,

Jag

Thanks,
Jag
Super User
Posts: 8,090

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

SAS has introduced the IFN and IFC function to do what you want.  Personally I would prefer to read code that just uses IF/THEN for these types of binary choices as it is easier to understand.

If you want to use a format you might want to use the OTHER option.

proc format ;

value gender 1='Male' 2='Female' other='Unknown' ;

run;

...

mf = put(sexe,gender.);

Occasional Contributor
Posts: 5

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

Sorry Tom,

Your answer was PERFECT! I did not notice the first sentence about IFN & IFC function. They do exactly what I'm expecting for.

Michel

Super User
Posts: 8,090

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

I still wouldn't use the IFN/IFC functions for normal programming.  To me they look too much like complex APL one liners that would be impossible to understand and maintain.  I suspect that you are used to writing expression in Excel cells where you cannot have programming statements.

tax =ifn(currency='Euro', .25*price, .10*price);

You can just write normal programming statements.

if (currency='Euro') then tax=.25*price;

else tax=.10*price ;

Occasional Contributor
Posts: 5

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

I almost agree with you... As I said earlier, that's just a matter of lazyness.

have a nice week end

PROC Star
Posts: 8,164

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

: Also in support of your advice, there appears to be something inefficient about the way ifc was written. It takes more than twice as long to process than a combination of if then else statements.

ifn at least processes in the same time as its if then else relative.

I tested the two with the following:

data testdata;

set sashelp.class;

do i=1 to 10000;

output;

end;

run;

data test1a;

set testdata;

gender=ifc(sex eq 'M','Male','Female');

run;

data test1b;

set testdata;

if sex eq 'M' then gender='Male';

else gender='Female';

run;

data test2a;

set testdata;

agegroup=ifn(age le 15,1,2);

run;

data test2b;

set testdata;

if age le 15 then agegroup=1;

else agegroup=2;

run;

Occasional Contributor
Posts: 5

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

... regarding my own process, CPU times are very close between If Then Else, IFC and IFN.

Let's says we can still use several tools for such operations before knowing if there is a best and a worst. That's the bright (although sometimes exhausting...) side of SAS!

Michel

Original site validation data Site name:    'CHU DE MONTPELLIER SERVEUR DE PROD VIRTUEL PMSI'.

Site number:  50100613. CPU A: Model name='' model

Operating System:  WX64_SV

...

data testdata; 16          set sashelp.class; 17          do i=1 to 50000; 18            output; 19          end; 20        run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.TESTDATA has 950000 observations and 6 variables. NOTE: Compressing data set WORK.TESTDATA decreased size by 17.06 percent.       Compressed is 9380 pages; un-compressed would require 11310 pages. NOTE: DATA statement used (Total process time):       real time          0.57 seconds       cpu time            0.57 seconds      21        22        23        data test1a; 24          set testdata; 25          gender=ifc(sex eq 'M','Male','Female'); 26        run; NOTE: There were 950000 observations read from the data set WORK.TESTDATA. NOTE: The data set WORK.TEST1A has 950000 observations and 7 variables. NOTE: Compressing data set WORK.TEST1A decreased size by 81.23 percent.       Compressed is 3639 pages; un-compressed would require 19388 pages. NOTE: DATA statement used (Total process time):       real time          1.44 seconds       cpu time            1.45 seconds       27        data test1b; 28          set testdata; 2 The SAS System                                                        09:49 Friday, January 4, 2013 29          if sex eq 'M' then gender='Male'; 30          else gender='Female'; 31        run; NOTE: There were 950000 observations read from the data set WORK.TESTDATA. NOTE: The data set WORK.TEST1B has 950000 observations and 7 variables. NOTE: Compressing data set WORK.TEST1B decreased size by 8.64 percent.       Compressed is 10333 pages; un-compressed would require 11310 pages. NOTE: DATA statement used (Total process time):       real time          1.20 seconds       cpu time            1.20 seconds      32        data test2a; 33          set testdata; 34          agegroup=ifn(age le 15,1,2); 35        run; NOTE: There were 950000 observations read from the data set WORK.TESTDATA. NOTE: The data set WORK.TEST2A has 950000 observations and 7 variables. NOTE: Compressing data set WORK.TEST2A decreased size by 21.78 percent.       Compressed is 10321 pages; un-compressed would require 13195 pages. NOTE: DATA statement used (Total process time):       real time          1.15 seconds       cpu time            1.15 seconds      36        data test2b; 37          set testdata; 38          if age le 15 then agegroup=1; 39          else agegroup=2; 40        run; NOTE: There were 950000 observations read from the data set WORK.TESTDATA. NOTE: The data set WORK.TEST2B has 950000 observations and 7 variables. NOTE: Compressing data set WORK.TEST2B decreased size by 21.78 percent.       Compressed is 10321 pages; un-compressed would require 13195 pages. NOTE: DATA statement used (Total process time):       real time          1.17 seconds       cpu time            1.17 seconds      ...

Posts: 3,852

## Re: is ther a logical function similar to "iif" that can be used into a Data step ?

Perhaps one reason the function is slower is each argument's expression is evaluated even if not true.  This is useful as Howard pointed out in his paper on conditional lag. http://www.nesug.org/proceedings/nesug07/cc/cc33.pdf

For obvious reasons IFC is perhaps most useful when called with %SYSFUNC.

🔒 This topic is solved and locked.