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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

OR Try any()

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

View solution in original post

10 REPLIES 10
Rick_SAS
SAS Super FREQ

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.

acordes
Rhodochrosite | Level 12

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 🙂

 

 

 

Rick_SAS
SAS Super FREQ

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.

Ksharp
Super User

OR Try any()

if any(result > 0) then Break_even = MIN(loc(result > 0));
acordes
Rhodochrosite | Level 12

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.

Rick_SAS
SAS Super FREQ

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. 

IanWakeling
Barite | Level 11

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

Rick_SAS
SAS Super FREQ

@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.

IanWakeling
Barite | Level 11

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.

 

Rick_SAS
SAS Super FREQ

An easier-to-read version might be 

if all(result=.) then 
   handle_all_missing;
else
   b = min(...);

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 10 replies
  • 3718 views
  • 5 likes
  • 4 in conversation