Announcement

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

  • Looping over values of a macro

    Hi,

    I am trying to assign my variables to local macros so that I can use them more easily in my models:

    local VS1 V1 V2 V3 V4

    local VS2 V5 V6 V7
    local VS3 V8 V9 V10

    V1 to V4 should always be in my model. But, I want to try adding two groups of variables to my control variables to check the robustness of the models. One group is V5 to V7 and the second group is V8 to V10.

    This is what I did:

    foreach v of varlist `VS2' `VS3'{
    display "This is the DiD using `v' as the imputation method"
    diff y , p( period) t(treat) cov(`VS1' `v') kernel id(id)
    }

    Models are run. But, the code is not doing what I want it to do. Insteda, it runs the model in the following order

    1- V1 V2 V3 V4 V5
    2- V1 V2 V3 V4 V6
    3- V1 V2 V3 V4 V7
    4- V1 V2 V3 V4 V8

    A solution was the following (expanding the macro twice)

    local VS1 V1 V2 V3 V4

    local VS2 V5 V6 V7
    local VS3 V8 V9 V10

    local u VS2 VS3
    foreach var of local u{
    display " This is the DiD using ``u'' as the imputation method"
    diff y , p( period) t(treat) cov(`VS1' ``v'') kernel id(id)
    }



    This worked. But, I still wonder why the first method does not do what the second method does.

    Thanks,
    Navid








  • #2
    Comments are in-line.

    Code:
    clear
    set more off
    
    *----- example variables and macros -----
    
    forvalues i = 1/10 {
        gen V`i' = .
    }
    
    local VS1 V1 V2 V3 V4
    local VS2 V5 V6 V7
    local VS3 V8 V9 V10
    
    *----- your first code -----
    
    // this block
    foreach v of varlist `VS2' `VS3' {
        display "`VS1' `v'"
    }
    
    // expands to
    foreach v of varlist V5 V6 V7 V8 V9 V10 {
        display as err "`VS1' `v'"
    }
    
    // so results are as expected
    
    *----- your second code -----
    
    local u VS2 VS3
    foreach var of local u {
        display as err "`VS1' ``v''"
    }
    
    // i don't see why you say this works. probably you are omitting
    // information
    
    *----- what you want -----
    
    foreach v in VS2 VS3 {
        display as err "`VS1' ``v''"
    }
    
    // works because in the first round through the loop,
    // ``v'' expands to `VS2' and then immediately
    // to V5 V6 V7.
    // in the second round ``v'' expands to `VS3' and
    // then immediately to V8 V9 V10
    See at least -help macro- and the corresponding manual entries.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment

    Working...
    X