Announcement

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

  • Loop over datasets

    Dear Stata users.
    I have 3 data sets and I would like to use a loop to run the same code on each of the 3 datasets. How can I do this?
    The datasets are saved as follows:
    13_LEZ_city_p (main var no2_tec)
    1_LEZ_city_a (main var pmtech)
    9_LEZ_city_lj (main var pmtech_2)

    So the only thing that changes in these datasets are the vars no2_tec, pmtech, and pmtech_2

    Code:
    ***
    cd "$uni_pcdata"
    use 13_LEZ_city_p, clear
    
    eststo clear
    eststo: quietly probit no2_tec regul equip  city_size age child health hhold i.city##i.year, cluster(idgroup)
    esttab, keep( regul equip  city_size age child health hhold) star(* 0.1 ** 0.05 *** 0.01) pr2 se
    predict auct13
    foreach X in regul equip  city_size age child health hhold{
    eststo: quietly probit no2_tec `X'  i.city##i.year if auct13!=., cluster(idgroup)
    }
    save lez13, replace
    foreach num of numlist 1(1)28 {
    use lez13, clear
    keep if city==`num'
    psmatch2 no2_tec regul equip  city_size age child health hhold i.year, out(pollutionN2)  common
    sort idgroup year
    save pss`num', replace
    }
    use pss1, clear
    foreach num of numlist 1(1)28  {
    append using pss`num'
    }
    cd "$uni_pcdata"
    save city_psm, replace
    cd "$uni_pcdata"
    use 13_LEZ_city, clear
    merge m:1 idgroup using city_psm
    save city_psm_new, replace
    ***
    Thank you
    JLi

  • #2
    To do something like this, in my experience it's easiest loop over the position within a macro rather than over the names.

    Say you create two locals with the datasets and the respective outcomes
    Code:
    local datasets data1 data2 data3
    local outcomes outcome1 outcome2 outcome3
    You can get the number of elements in the list (potentially you should check whether both local have the same length)
    Code:
    local n_datasets : word count `datasets'
    and then loop over the number of datasets and extract the relevant outcome and dataset by the index position within the macro
    Code:
    forval d = 1/`n_datasets' {
    local this_dataset `: word `d' of `datasets''
    local this_outcome `: word `d' of `outcomes''
    }
    Now you can use `this_dataset' and `this_outcome' within the loop, which in each turn will have the d'th element of `datasets' or `outcomes'.

    Full example to try out below

    Code:
    local datasets data1 data2 data3
    local outcomes outcome1 outcome2 outcome3
    
    local n_datasets : word count `datasets'
    local n_outcomes : word count `outcomes'
    assert `n_datasets' == `n_outcomes'
    
    forval d = 1/`n_datasets' {
        local this_dataset `: word `d' of `datasets''
        local this_outcome `: word `d' of `outcomes''
        
        disp "the dataset is `this_dataset' and the outcome is `this_outcome'"
    }
    Last edited by Henry Strawforrd; 29 Mar 2022, 03:39.

    Comment


    • #3
      Thanks Henry!

      Comment

      Working...
      X