Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing a local macro with string content

    Hello Statalisters,

    I apologize if this has been addressed elsewhere -- I've been working on this problem off and on for several weeks and (I think) have reviewed all posts and manual sections that touch on the issue I'm having, without success

    I'm in stata 14.2, and for months have been having trouble getting any local containing text to work. For example, I try to write a local to define my cd, as follows

    Code:
    local ddir "C:\Users\blablabla"
    But when I type
    Code:
    di `ddir'
    nothing happens

    I've worked around this till now.
    I now have a survey with several questions in a matrix, all with the same yes/no response options (yes =1, no = 2). It looks something like this:

    If you went to the grocery store today, would you
    1) buy eggs, yes/no
    2) buy butter, yes/no
    3) buy vegetables, yes/no

    Currently, variable representing question 1 is v_1, var representing question 2 is v_2, etc.
    I want to write a loop that renames all of these variables "grocery_eggs", "grocery_butter", etc

    I tried the following code, based on a prior post on this topic:

    Code:
    label define yesno 1 "Yes" 2 "No"
    local grocery "eggs butter veg"
    local n : word count `grocery'
    
    forvalues i = 1/`n'    {
        foreach var of varlist v_1-v_3    {
                gen `var'l = `var'
                label values `var'l yesno
                forvalues i = 1/`n'        {
                    local a : word `i' of `grocery'
                    label `var'l "grocery_"`a'
                    }
                }
    It doesn't work, and when i troubleshot by trying
    Code:
    di `grocery'
    , the same thing happened as above -- nothing displayed
    I tried double quotes in both the local definition, ie
    Code:
    local grocery "`eggs butter veg'"
    and
    Code:
    local grocery `"eggs butter veg"'
    as well as in the display command involving the local with string, ie
    Code:
    di `"grocery"'
    and
    Code:
    di "`grocery'"
    and neither worked

    Thank you very much in advance!
    -Beth

Working...
X