|Shortcuts to refer multiple variables
|Paul Lin, StataCorp
Understand that whenever Stata wants a varlist it can be a list of variables, such as
. list length turn
or it can be all variables starting with a certain prefix
. list rep*
(meaning all variables named "rep" followed by something), or it can be a range of variables
. list mpg-weight
(meaning all variables mpg through weight in the order that the variables are recorded in the dataset).
You can even combine all three syntaxes:
. list length turn rep* mpg-weight
The only thing Stata is missing is an easy way to specify variables v1, v2, .... When the names are in common, the variables are numbered sequentially, but they are not stored sequentially. For instance, pretend that the order of the variables in the dataset is
1. v1 2. alpha 3. v3 4. beta 5. v4 6. v2 7. gamma
. list v1-v4
is equivalent to typing
. list v1 alpha v3 beta v4
and is not equivalent to typing
. list v1 v2 v3 v4
Here you want to consider reordering the variables in your dataset. order, sequential will put the variables in alphabetical order (and does mostly smart things with numeric suffixes). In the above example, if I type
. order *, sequential
the resulting order is
1. alpha 2. beta 3. gamma 4. v1 5. v2 6. v3 7. v4
order, sequential is smart enough to know that v10 comes after v9 and not between v1 and v2, which pure alphabetical order would specify. For online help, type help order in Stata, or see [D] order.
Classroom and web training
Teaching with Stata
Statalist: The Stata Forum
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.