Search code examples
regexstatastata-macros

How to extract unique strings from a macro?


I'm trying to automate a reshape using Stata.

I have a series of variables measured yearly. They are all named varname_yy, where yy is a number referring to the year of measurement. I managed to extract all the stubs varname_ from the variables and to put them into a macro using the following code:

local stubs
foreach var of varlist `myvars' {
local stub = substr("`var'",1,length("`var'") - 2)
    local stubs `stubs' `stub'
}

The problem is that I end up with many repeated stubs in the stubs macro and this causes reshape to return an error message.

In R I would just ask for unique(stubs), but I couldn't find any such function in Stata.

My tentative solution is to do the following:

local uniquestubs
foreach stub in `stubs' {
    if !regexm("`uniquestubs'","`stub'") {
        local uniquestubs "`uniquestubs'" " `stub'"
    }
}

However, I can't get rid of the duplicates.

What is the correct way of doing it?


Solution

  • Roberto Ferrer's comment to my question led me to the answer. Turns out that all I had to do was to use the following line:

     local uniquestubs: list uniq stubs
    

    Unlike syntax for evaluating macros, manipulating macros requires the macro names, so I should not enclose stubs in ` and ' as it is usually done.