Search code examples
loopsstatastata-macros

Generate a new macro prefixing words of an old macro


Define a macro try:

local try "a b c"

This will generate a macro with the value a b c.

Now I want a new macro try2 which takes the value da db dc. That is, I want to add the same prefix to each element of the old macro and define it as a new macro.


Solution

  • There is, so far as I know, no function that specifically supports that in official Stata. For nearby functions, see help macrolists.

    An old package listutil (SSC) includes various commands, prelist being pertinent. I wrote that, so it's not being negative about others' work that makes me recommend just applying general technique.

    local try "a b c" 
    local copy `try' 
    gettoken first copy : copy 
    
    while ("`first'" != "") { 
        local try2 `try2' d`first' 
        gettoken first copy : copy 
    } 
    
    di "try is {col 12} `try'"  
    di "try2 is{col 12} `try2'"