Calcite | Level 5

## Choose function vs. if-then do

I have a piece of my program that has some nested if-then statements. I read in Rick Wicklin's "Statistical Programming with SAS/IML Software" that you should use the choose function when you are assigning values based on certain criteria. I was wondering if the choose function is always the more efficient way since I would be putting choose functions within choose functions. I know it cuts down on the amount of code written, but is there a processing time difference? An example of my code is below. Any help is appreciated. Thank you!

PP_ind = loc(Line = 'PP');

if ncol(PP_ind) > 0 then do;

Guar[PP_ind] = round(App[PP_ind] # Level * .60, .1);

PP_PF_ind = loc(Line = 'PP' & Option = 'PF');

if ncol(PP_PF_ind) > 0 then

Guar[PP_PF_ind] = round(App[PP_PF_ind] # Level * .65, .1);

PP_PT_ind = loc(Line = 'PP' & Option = 'PT');

if ncol(PP_PT_ind) > 0 then

Guar[PP_PT_ind[ = round(App[PP_PT_ind] # Level * .70, .1);

end;

Proposed method using the choose function - not sure if it is the correct syntax because I am getting an error: "ERROR: The arguments to the built-in function are invalid. (444, 22)"

Guar = choose(Line = 'PP', choose(option = 'PF', round(App # Level * .65, .1), choose(option = 'PT', round(App # Level * .70, .1), round(App # Level * .60, .1))))

Maybe my question should be - "Is it possible to put a choose function inside of another choose function?"

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: Choose function vs. if-then do

Yes, you can put a CHOOSE function inside another.

If you are only doing this choice a few times, then it doesn't matter which conditional statements you use, and I'd choose the IF-THEN because it is easier to read. If this is going to be inside a big loop, then you might want to compare the performance by timing the computations. See Remove or keep: Which is faster? - The DO Loop or Chapter 15 of Statistical Programming with SAS/IML Software.

Regarding your nested CHOOSE construction, you are missing a final 'else'.  What happens if Line^='PP'? It should read:

Guar = choose(Line = 'PP', choose(...), SOMEVALUE );

A trick that I like to use is to precompute the nested values so that the nested CHOOSE functions aren't so hard to read. Something like this might be helpful if you 'choose' to use nested CHOOSE functions:

/* possible results */
R1 = round(App # Level * .65, .1);
R2 = round(App # Level * .70, .1);
R3 = round(App # Level * .60, .1);

A = choose(option = 'PT', R2, R3);
B = choose(option = 'PF', R1, A);
Guar = choose(Line = 'PP', B, SOMEVALUE);

SAS Super FREQ

## Re: Choose function vs. if-then do

Yes, you can put a CHOOSE function inside another.

If you are only doing this choice a few times, then it doesn't matter which conditional statements you use, and I'd choose the IF-THEN because it is easier to read. If this is going to be inside a big loop, then you might want to compare the performance by timing the computations. See Remove or keep: Which is faster? - The DO Loop or Chapter 15 of Statistical Programming with SAS/IML Software.

Regarding your nested CHOOSE construction, you are missing a final 'else'.  What happens if Line^='PP'? It should read:

Guar = choose(Line = 'PP', choose(...), SOMEVALUE );

A trick that I like to use is to precompute the nested values so that the nested CHOOSE functions aren't so hard to read. Something like this might be helpful if you 'choose' to use nested CHOOSE functions:

/* possible results */
R1 = round(App # Level * .65, .1);
R2 = round(App # Level * .70, .1);
R3 = round(App # Level * .60, .1);

A = choose(option = 'PT', R2, R3);
B = choose(option = 'PF', R1, A);
Guar = choose(Line = 'PP', B, SOMEVALUE);

From The DO Loop