create a variable list from local macro list (findname)

2k views Asked by At

I have lists of variables in local macros that I would like to use in a command, e.g.:

    local a int1 int2 num1 num2 bin1 bin2 ...
    local b int3 int4 bin3 num3  ...

Here variable num* int* and bin* are numeric, integer and binary respectively.

I want to run a foreach loop over of the variables in local a' and b', but only those that are integer-valued, namely, int1, int2, int3, ...

For this, I use findname command created by Nick Cox to find variables that are integer-valued, saving a list of corresponding variable names in a local macro. But I do not want to run it for all variables in a dataset but only for subsets of variables (designated in locals a, b, and so on), and then use the list in a subsequent command.

So for a list of integer variables in local a' and b' being local a_int' and b_int', I want to use is as below:

    foreach var of varlist `a_int' `b_int'{
        whatever command
    }
1

There are 1 answers

0
Wouter On BEST ANSWER

If I understand you correctly, you need the intersection of two lists stored in local macros. To do this you can use the macro extended functions to manipulate lists: see help macrolists.

// Setup
local a x1 x2 x3 x4 x5
local b x4 x5 x6 x7 x8
local int x2 x3 x6

// Get intersection of variable lists and integer list
local a_int : list a & int
local b_int : list b & int

// Put a_int and b_int together and drop possible duplicates
local ab_int `a_int' `b_int'
local ab_int_uniq : list uniq ab_int

// Do something
foreach var of varlist `ab_int_uniq' {
    * do something
}