0

I need to use a local macro to loop over part of a variable name in Stata.

Here is what I tried to do:

local phth mep mibp mbp
tab lod_`phth'_BL

Stata will not recognize the entire variable name.

variable lod_mep not found
r(111);

If I remove the underscore after the `phth' it still does not recognize anything after the macro name.

I want to avoid using a complicated foreach loop.

Is there any way this can be done just using the simple macro? Thanks!

pakalla
  • 163
  • 4
  • 10
  • 2
    Stata won't recognize the entire variable name simply because you have passed the entire local to the command: `tab lod_mep mibp mbp_BL`. So unless you have variables named `lod_mep`, `mibp` and `mbp_BL` this will break (in fact, it breaks with `tab` regardless because there would be 3 variables). Macros are not used to loop over the elements of the list - for that you need a loop as @Brendan Cox points out. – ander2ed Oct 15 '15 at 22:41
  • To be clear, the result I want is the following: tab lod_mep_BL tab lod_mibp_BL tab lod_mbp_BL – pakalla Oct 15 '15 at 23:31

1 Answers1

3

Your request is a bit confusing. First, this is precisely the purpose of a loop, and second, loops in Stata are (at the "introductory level") quite simple. The following example is a bit nonsensical (and given the structure, there are easier ways of going about this), but should convey the basic idea.

// set up a similar variable name structure
sysuse auto , clear
rename (price mpg weight length) /// 
       (pref_base1_suff pref_base2_suff pref_base3_suff pref_base4_suff)

// define a local macro to hold the elements to loop over
local varbases = "base1 base2 base3 base4"

// refer to the items of the local macro in a loop
foreach b of local varbases {
    summ pref_`b'_suff
}

See help foreach for the syntax of foreach. In particular, note that the structure employed above may not even be required due to Stata's varlist structure (see help varlist). For example, continuing with the code above:

foreach v of varlist pref_base?_suff {
    summ `v'
}

The wildcard ? takes the place of one character. * could be used for more flexibility. However, if your variables are not as easily identifiable using the pattern matching allowed by varlist, a loop as in the first example is simple enough -- four very short lines of code.


Postscript

Upon further reflection (sometimes the structure of the question anchors a certain method, when an alternative approach is more straightforward), searching the help files for information on the tabulate command (help tabulate) will direct you to the following syntax: tab1 varlist [if] [in] [weight] [, tab1_options]

Given the discussion above about the use of varlists, you can simply code

tab1 lod_m*_BL

assuming, of course, that there are no other variables matching the pattern for which you do not want to report a frequency table. Alternatively,

tab1 lod_mep_BL lod_mibp_BL lod_mbp_BL

is not much longer and does the trick, albeit without the use of any sort of wildcard or macro substitution.

Brendan
  • 3,901
  • 15
  • 23
  • Nice answer. Also note the difference between `tab` and `tab1`. The former will not work with three variables while the latter will – ander2ed Oct 16 '15 at 02:00