Q:

How to test if a variable exists

I need to run SPSS syntax in an IF statement, which tests if a variable exists in the document.  I am having trouble getting the IF test right.  I'm trying to do this:
do if (test if myVariable exists).
// execute code here
end if.
Execute.

I've looked here and tried this:
DO IF (myVariable) exist=1.
END IF.
Execute.

But I get the error 'There is extraneous text following the logical expression on a DO IF command.  Have I misunderstood the code?

A:

spssinc select variables command creates a list of variables according to a specified propertie. In this case the property will be the variable called "MyVar". If the variable doesn't exist, the list will stay empty:
spssinc select variables macroname="!findMyVar" /properties pattern="MyVar".

Now we define a macro that will run some commands only if the above list is not empty:
define doifMyVarexists ()
!if (!eval(!findMyVar)<>"") !then
  * put your commands here, like following examples.
  compute MyVar=MyVar*2.
  freq MyVar.
!ifend
!enddefine.

* the macro is ready, now we call it:
doifMyVarexists.

If you run this multiple times, you will face a problem that if MyVar exists once and in a later run doesn't exist - the list doesn't get emptied (it is only overwritten if there were variables to put into it).
To solve that use the following line to empty the list before running select variables again:
define !findMyVar() !enddefine.

