Locals containing a list of variables can be very useful when using Stata. A common need is a local containing all variables of a data set. This local can be created by means of the ds command.
Here is an example using the lifeexp.dta data file.
. webuse lifeexp, clear
(Life expectancy, 1998)
Now, let’s create a local named allvar that will contain all variables of this data set.
. ds
region country popgrowth lexp gnppc safewater
. local allvar `r(varlist)’
. di “`allvar'”
region country popgrowth lexp gnppc safewater
We can see that ds stored the variable list into r(varlist). One interesting variation is the creation of a local containing all variables except region. You will need to specify the variables to be escluded right after ds, and add the option not after a comma.
. ds region, not
country popgrowth lexp gnppc safewater
. local othervar `r(varlist)’
. di “`othervar'”
country popgrowth lexp gnppc safewater
The command ds has several other useful applications that will be commented later in this blog.