03-03-2017 03:19 PM
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
03-03-2017 04:07 PM
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);
/* 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.
03-03-2017 05:03 PM
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
03-03-2017 05:09 PM
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.
03-06-2017 06:50 AM
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.
03-06-2017 07:17 AM
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.
03-07-2017 05:37 AM
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
03-07-2017 05:51 AM
03-07-2017 06:09 AM
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.