In a program I am writing I want the same utility file to be run on different datasets and certain variables in the dataset. But the datasets have different sets of variables. So a nice Stata tip, adapted for my purpose, was provided by Martin Weiss via Stata listserv.
capture confirm variable name3
if !_rc {
loc vars_to_use "name1 name2 name3"
}
else {
loc vars_to_use "firstname lastname"
}
* run utility file
foreach x in `vars_to_use' {
do something to `x'
}