Search
   >> Home >> Resources & support >> FAQs >> Shortcuts to refer multiple variables

How can I list, drop, and keep a consecutive set of variables without typing the names individually?

Title   Shortcuts to refer multiple variables
Author Paul Lin, StataCorp
Date March 1997; minor revisions July 2011

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

Then typing

        . 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.

The Stata Blog: Not Elsewhere Classified Find us on Facebook Follow us on Twitter LinkedIn Google+ Watch us on YouTube