Search code examples
stringstatalocalstata-macros

Find union of two variable-name scalars


I have a Stata program that outputs a local scalar of space-separated variable names.

I have to run the program twice on two samples (same dta) and store the union (intersection - variable names appearing in both scalars) as a new space-separated local scalar (for input to another program).

I can't figure out how to split (per spaces) and or test the occurrences of variable names in each.


Solution

  • Stata has a bunch of extended macro functions to use on lists that you can find with help macrolists, where you can see that A & B returns the intersection of A and B. If A="a b c d" and B="b c f g", then A & B = "b c".

    This allows you to do something like this:

    clear
    scalar l1="vara varb varc"
    scalar l2="varc vard vare"
    local l1 = scalar(l1)
    local l2 = scalar(l2)
    local inter: list l1 & l2
    scalar inter="`inter'"
    scalar list inter
    

    You convert the scalars to locals, get their union, and convert that into a scalar. It is probably easier to just modify your code to use locals rather than scalars so you don't have to deal with conversions.