Stata tip: Doing something conditional on existence of a variable in the dataset, using a local

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'
   }

About mkevane

Economist at Santa Clara University and Director of Friends of African Village Libraries.
This entry was posted in R statistics. Bookmark the permalink.