Announcement

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

  • Merging using loop

    Dear all,

    I am trying to merge files in one folder with files in another folder (all stata files). Below is the codes I have used. I am getting "invalid syntax" . Will be very grateful for help on this. Thank you very much.


    global original "/research/alldata_stataversions/data"
    global working "/data file/new"

    local files : dir "$original/folder1/" files "*.dta"
    foreach file in `files' {

    use "$original/folder1/`file'",clear
    display `files'

    local usingfile: dir "$original/folder2/" files "*.dta"
    foreach u in `usingfile' {
    cd "$original/folder2/`usingfile'"
    merge 1:m id using "/`u'"

    }
    }

  • #2
    This line makes no sense:

    Code:
    cd "$original/folder2/`usingfile'"
    becasue according to your code usingfile is a list, not an individual subdirectory that you may wish to use in cd.

    Consider this exerpt:


    Code:
    set trace on
    local usingfile: dir "C:\Program Files\Stata17\ado\base/n/" files "*.dta"
    foreach u in `usingfile' {
        cd "/`usingfile'"
        //merge 1:m id using "/`u'"
    }
    As the above code demonstrates this results in error 198 "invalid syntax" when trying to execute:
    Code:
    = cd "/"network1.dta" "network1a.dta" "nlsw88.dta" "nlswide1.dta""
    Whether this is the only error in the code and what is the intended action is not entirely clear to me, but hope this helps.

    Best, Sergiy Radyakin

    Comment

    Working...
    X