0
votes

I would like to enter groups of variables into a Stata command, but can't find a way to do so.

E.g. In a factor analysis, with a set of 41 variables, I would like to exclude the 5th, 33rd and 35th, but include the rest.

Should it be something like: factor x1-x4, x6-x32, x34, x36-41, factors(5) pcf

1

1 Answers

1
votes

Your example calls up a factor analysis. Let''s keep with that. If your variables are indeed at least those named out of x1 through x41 then

 factor x1-x4 x6-x32 x34 x36-x41 

could be legal. Note that (1) the commas are not included; (2) the last varlist was corrected, as x36-41 could never be a legal varlist (as 41 could never be a legal varname); and (3) when two or more variable names are joined with a hyphen, here x6-x32 and x36-x41, such a varlist indicates a block of variables in the current dataset order, not necessarily all variables whose names begin with x with implied suffixes, e.g. in 36(1)41. Thus x36-x41 could mean x36 frog toad x41 if you have variables with those names in that order.

The moral is simple: have your variables in an order that makes management and analysis simple and easy to think about. The order command provides the easiest way to change variable order programmatically.

The more general problem of removing variable j in order from an arbitrary varlist seems a little artificial, but here we go. Suppose we have a list of variable names (in fact any names) in a local macro. tokenize maps them one by one to local macros numbered 1 up, after which we can remove whatever we like. In the example below the output of mac li is edited to remove stuff irrelevant to this example, which could be quite a lot.

. local varlist foo bar bazz frog toad newt whatever

. tokenize `varlist'

. mac li
_7:             whatever
_6:             newt
_5:             toad
_4:             frog
_3:             bazz
_2:             bar
_1:             foo
_varlist:       foo bar bazz frog toad newt whatever

. foreach j in 1 3 5 {
  2. local varlist : list varlist - `j'
  }

. mac li
_varlist:       bar frog newt whatever
_7:             whatever
_6:             newt
_5:             toad
_4:             frog
_3:             bazz
_2:             bar
_1:             foo

For other methods of manipulating lists, see help macrolists.