Existence

Top  Previous  Next

One can use a $If or $Ifi with or without the not modifier to test for whether or not the control variable has been set and if so execute a command otherwise skipping that command.  The test to see if a control variable has been set is of one of three forms:

 

$If set       controlvariablename statementtoexecute;

$If setglobal controlvariablename statementtoexecute;

$If setlocal  controlvariablename statementtoexecute;

$If setenv    envronmentvariablename statementtoexecute;

 

The evaluation of each as to whether it is true or false depends on the characteristics of the set command used in its original definition.  Several cases arise:

If the variable identified by controlvariablename or envronmentvariablename was set by any of the $set, $setenv, $setlocal, or $Setglobal commands it will render the $If involving the plain set conditional to be true.
If the control variable was established using $Setglobal, then any conditional involving the setglobal test will also be true.
If the control variable was established using $setlocal, then any conditional using the setlocal condition would become true.
If the control variable was not established with any of these statements it will not pass any of the conditionals.
$If or $Ifi can be used interchangeably with the not modifier included if desired.

Example:

(basicif.gms)

$setlocal  alocal yes

$set       aset yes

$Setglobal aglobal yes

acronyms local,global,plainset

scalar type;

*set item

$If set       aset   type=plainset;

$If setglobal aset   type=global;

$If setlocal  aset   type=local;

*setglobal

$If set       aglobal type=plainset;

$If setglobal aglobal type=global;

$If setlocal  aglobal type=local;

*setlocal

$Ifi set       alocal type=plainset;

$Ifi setglobal alocal type=global;

$Ifi setlocal  alocal type=local;

*not set

$If set       qq type=plainset;

$If setglobal qq type=global;

$If setlocal  qq type=local;

 

$If not set       qq display "No qq around";

 

results in the echo print

 

  17  *set item

  18  type=plainset;

  21  *setglobal

  22  type=plainset;

  23  type=global;

  25  *setlocal

  26  type=plainset;

  28 type=local;

  29  *not set

  34  display "No qq around";

 

where line 18 shows that the command variable established with the $set  the syntax passes only the $If set conditional while the setglobal command variable passes both the $If set and the $If setglobal conditionals.  Also $setlocal command variable passes both the $Ifi set and the $Ifi setlocal condition.  Finally, the referenced command variable qq that was never established within a set command did not pass any of the conditions except the last one with the not in it.

 

Examples employing the environmental variables appear in environvar.gms.