Fluorite | Level 6

## How do I apply element-wise condition within optimisation procedure in proc IML

Hi,

I'm working within Proc IML and have already set up a maximum likelihood function and using nlpnrr method to optimise. The optimisation itself works well. The problem is when I added certain If-Then_else statements within this optimization procedure. I need to put condition on certain elements/observations in the matrices. For example,

if income[,m] < 0 then income[,m]=0; else income[,m]=income[,m];

The rows of the matrix are individuals and m is the number of  ways that I calculate income in the previous lines through a do loop..

I noticed that when I put this condition, the way SAS reads it is - if income is negative for ALL the elements in each column then it'll give zeros to ALL the individuals in that column, otherwise it keeps income as it is for everyone. However, what I need to see is to only give 0 to those observations that have a negative income. When I add "i" in the above code so that;

do i=1 to num;  /*num is number of observations*/

if income[i,m] < 0 then income[i,m]=0; else income[i,m]=income[i,m];

end;

this works but the problem is that this code doesn't work in optimisation procedure and I get error.

So my question is that how can I do this within the nlpnrr optimisation procedure? I appreciate any help..

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: How do I apply element-wise condition within optimisation procedure in proc IML

First read the article "IF-THEN logic with matrix expressions", which explains that the condition

if income[,m] < 0 then...

is equivalent to

if ALL( income[,m] < 0 ) then...

.

It sounds like you want to use a different logical condition:

if ANY( income[,m] < 0 ) then...

If that is correct, then you can use the LOC function or the CHOOSE function. In either case, you get rid of the IF-THEN statement.

To use the LOC function, see the article "Finding matrix elements that satisfy a logical expression". (But be sure to make sure at least one observation meets the condition.)

Alternatively, you can use the CHOOSE function, which has a slightly simpler syntax, as follows.

income[,m] = choose( income[,m] < 0,  0,  income[,m] );

2 REPLIES 2
SAS Super FREQ

## Re: How do I apply element-wise condition within optimisation procedure in proc IML

First read the article "IF-THEN logic with matrix expressions", which explains that the condition

if income[,m] < 0 then...

is equivalent to

if ALL( income[,m] < 0 ) then...

.

It sounds like you want to use a different logical condition:

if ANY( income[,m] < 0 ) then...

If that is correct, then you can use the LOC function or the CHOOSE function. In either case, you get rid of the IF-THEN statement.

To use the LOC function, see the article "Finding matrix elements that satisfy a logical expression". (But be sure to make sure at least one observation meets the condition.)

Alternatively, you can use the CHOOSE function, which has a slightly simpler syntax, as follows.

income[,m] = choose( income[,m] < 0,  0,  income[,m] );

Fluorite | Level 6

## Re: How do I apply element-wise condition within optimisation procedure in proc IML

Thanks Rick. I used the ‘choose’ function as you siggested and it worked. Thanks so much.
From The DO Loop