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

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
Rick_SAS
SAS Super FREQ

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

View solution in original post

1 REPLY 1
Rick_SAS
SAS Super FREQ

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-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 1285 views
  • 0 likes
  • 2 in conversation