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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s