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

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

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

 

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

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

 

ZMX
Fluorite | Level 6 ZMX
Fluorite | Level 6
Thanks Rick. I used the ‘choose’ function as you siggested and it worked. Thanks so much.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 870 views
  • 1 like
  • 2 in conversation