Announcement

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

  • Looping through a list from a file

    Dear Statalist members,

    I would like to loop through a list of approximately 500 items. How do I read in those items to create a local list?

    For context (perhaps there is a different solution to the problem): I'm importing excel files that sometimes start in row2, sometimes in row3. I read in all of them assuming that they start in row2 and created a file with those that did not:

    local files : dir "E:\tomerge" files "*.xlsx"

    foreach file in `files' {
    import excel using `file', cellrange(A2) firstrow allstring clear
    local VARIABLE ="`file'"
    display "`VARIABLE'"
    g name = "`VARIABLE'"
    duplicates drop
    append using `master', force
    duplicates drop
    save `master', replace
    }

    preserve

    drop if Issuer !=""
    duplicates drop name, force
    keep name
    save missing, replace

    Now I would like to read these missing files into a new local list so that I can import them - but how?

    foreach file in `missing' {
    import excel using `file', cellrange(A3) firstrow allstring clear

    I'm using Stata/MP17.0 on Windows.

    Thank you in advance,
    Moqi

  • #2
    Now I would like to read these missing files into a new local list so that I can import them - but how?
    Code:
    use missing, clear
    levelsof name, local(missing)
    will place a list of the missing files in local macro missing.

    See -help levelsof- for more information about this extremely useful command.

    Comment

    Working...
    X