I would like to write a program which loads a dataset and then only keeps variables specified in an if condition, before proceeding with further analysis in the program.
The issue is that it seems [if] in syntax checks whether the variables exist at the time the program is called. Therefore I cannot run the program unless the data happens to already be open. Minimal working example:
This will only work if auto.dta happens to already be open when I call the program. Any clever quick work-arounds which allow me to keep the nice structure of a standard "if" expression in my program call?
The issue is that it seems [if] in syntax checks whether the variables exist at the time the program is called. Therefore I cannot run the program unless the data happens to already be open. Minimal working example:
Code:
* define the program
cap program drop example
program define example
syntax [if]
sysuse auto.dta, clear
keep `if'
// more analysis...
end
* call the program
example if foreign == 1

Comment