BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MLehmann
Calcite | Level 5

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)

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

View solution in original post

10 REPLIES 10
Jagadishkatam
Amethyst | Level 16

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.

Thank you your post.

Thanks,

Jag

Thanks,
Jag
MLehmann
Calcite | Level 5

Hi M. Jagadishkatam

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

Jagadishkatam
Amethyst | Level 16

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
Tom
Super User Tom
Super User

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.);

MLehmann
Calcite | Level 5

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

Tom
Super User Tom
Super User

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.

Instead of

  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 ;

MLehmann
Calcite | Level 5

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

have a nice week end

art297
Opal | Level 21

: 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;

MLehmann
Calcite | Level 5

... 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      ...

data_null__
Jade | Level 19

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2343 views
  • 9 likes
  • 5 in conversation