Statistical programming, matrix languages, and more

iml avoid error with empty matrix

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

iml avoid error with empty matrix

I run into an error while assigning a scalar with the loc function. 

Everything works fine until I do not meet the condition in the loc.

 

Break_even = MIN(loc(result > 0));

 

This returns the month when the project breaks even. But if the project fails to do so, the code stops executing because the matrix Break_even isn't set. 

I use this inside a Do Loop and I have tried to control this situation with If-then. 

without success so far. 

How do I handle situations like this?

 

Thank you for your help, bye, Arne

 

 


Accepted Solutions
Solution
‎03-06-2017 06:31 AM
Grand Advisor
Posts: 9,576

Re: iml avoid error with empty matrix

OR Try any()

if any(result > 0) then Break_even = MIN(loc(result > 0));

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,406

Re: iml avoid error with empty matrix

using the LOC function as an index without checking that the return value is nonempty is jokingly called "using a naked loc". Read about the naked loc and how to test for the condition.

 

The typically usage is

idx = loc(result > 0);

if ncol(idx) > 0 then 

   Break_even = min(idx);

else

   /* handle the case where no result is positive */

 

How you handle the empty result is up to you. You might assign a missing value, abort the program, assign a default value, and so forth.

Occasional Contributor
Posts: 16

Re: iml avoid error with empty matrix

Thank you very much.

I have applied your proposal 

BUT I still get the error message 

ERROR: ELSE does not follow IF statement at line=...

ERROR: END does not occur within DO group at line...

 

As I had mentioned, I use it within a Do Loop and making use of a SUBMIT statement I call SGPLOT to produce 64 graphs according to my different scenerios. It works perfectly except this abort when the there's no break even Smiley Happy

 

 

 

SAS Super FREQ
Posts: 3,406

Re: iml avoid error with empty matrix

The errors indicate that you did not implement the IF-THEN/ELSE condition correctly.  If you post the relevant portion of your log, someone will be able to find the mistake.

Solution
‎03-06-2017 06:31 AM
Grand Advisor
Posts: 9,576

Re: iml avoid error with empty matrix

OR Try any()

if any(result > 0) then Break_even = MIN(loc(result > 0));
Occasional Contributor
Posts: 16

Re: iml avoid error with empty matrix

Thank you.

It works except when I run an if-then statement within a do-loop.

Do I have to obey something special when I place an if-then logic within a do-loop?

The do-loop executes fine without if-then and the same is true for the inverse.

But both togehther gives error.

SAS Super FREQ
Posts: 3,406

Re: iml avoid error with empty matrix

No, the syntax for an IF-THEN statement inside a DO loop is the same as outside.  You might have missing or mismatched DO-END statements.

 

Please post the portion of the SAS log that gives the source and error messages. 

Frequent Contributor
Posts: 130

Re: iml avoid error with empty matrix

If you know the size of the matrix result, or can easily work it out,  then another way might be to use CHOOSE.  Here I am assuming result is a row vector with 12 elements, one for each month.

 

   b = min( choose(result>0, 1:12, . ));

 

I was thinking I would get a missing value returned when all the elements of result are negative, however I get the largest representable floating point.    This is in 13.1

SAS Super FREQ
Posts: 3,406

Re: iml avoid error with empty matrix

@IanWakeling Since the MIN function ignores missing values, you are getting the default initialized value, as documented. See the second paragraph of the doc of the MIN function.

Frequent Contributor
Posts: 130

Re: iml avoid error with empty matrix

Thanks Rick.   I should have checked the docs more carefully.  I think I will stop there, as adding something like

 

if b < constant('BIG') then...

will start to look very confusing.

 

SAS Super FREQ
Posts: 3,406

Re: iml avoid error with empty matrix

An easier-to-read version might be 

if all(result=.) then 
   handle_all_missing;
else
   b = min(...);
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 395 views
  • 5 likes
  • 4 in conversation