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

Here's a very odd glitch with CHOOSE that I discovered recently. Try this:

a = J(1,6,0);
b = J(1,6,1);
i = 2;
a = choose(i=1,b,a//b);

In words, if i = 1, I want 'a' to become 'b', otherwise, I want 'b' to be appended to 'a'. Doesn't work.

Now, replace the CHOOSE statement in the code above with the following equivalent if/then/else statement:
if i=1 then a = b; else a = a//b;

It will work. Why doesn't it work when using the CHOOSE function ?

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I wouldn't say it's a "glitch." As I point out in my blog post

http://blogs.sas.com/content/iml/2011/08/15/complex-assignment-statements-choose-wisely/,

you shouldn't think of CHOOSE as equivalent to the IF-THEN/ELSE statement, although they are similar.

The general formulation of the CHOOSE statement is to have three vectors as arguments. As I say in my blog:
"The CHOOSE function examines each element of the first argument. If an element is nonzero, it returns the corresponding element of the second argument. If an element is zero, it returns the corresponding element of the third argument."

In the general case, therefore, the dimensions of the three arguments must be the same. That's why the CHOOSE function is complaining that the second and third arguments are different dimensions. However, in your example the first argument is a scalar. Although CHOOSE could handle this case differently than the general case, that's not how it works, because special-casing certain behaviors can lead to other problems.  For example, the statement C = choose(i=2, ., A) is valid syntax. In your scheme, it's not clear whether  C should be a vector of missing values, or just a scalar missing value.

View solution in original post

1 REPLY 1
Rick_SAS
SAS Super FREQ

I wouldn't say it's a "glitch." As I point out in my blog post

http://blogs.sas.com/content/iml/2011/08/15/complex-assignment-statements-choose-wisely/,

you shouldn't think of CHOOSE as equivalent to the IF-THEN/ELSE statement, although they are similar.

The general formulation of the CHOOSE statement is to have three vectors as arguments. As I say in my blog:
"The CHOOSE function examines each element of the first argument. If an element is nonzero, it returns the corresponding element of the second argument. If an element is zero, it returns the corresponding element of the third argument."

In the general case, therefore, the dimensions of the three arguments must be the same. That's why the CHOOSE function is complaining that the second and third arguments are different dimensions. However, in your example the first argument is a scalar. Although CHOOSE could handle this case differently than the general case, that's not how it works, because special-casing certain behaviors can lead to other problems.  For example, the statement C = choose(i=2, ., A) is valid syntax. In your scheme, it's not clear whether  C should be a vector of missing values, or just a scalar missing value.

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
  • 730 views
  • 0 likes
  • 2 in conversation