Announcement

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

  • Loop on Insheet

    Good Morning,

    I want to import in STATA (and save as .dta with the prefix "geo_") several .txt files, all contained in a directory. Therefore, I've written this loop:

    cd "C:\Users\MyLocalDirectory\"
    local myfilelist : dir . files "*.txt"
    foreach file of local myfilelist {
    insheet using `file'
    save "geo_`file'$out_suffix", replace
    }

    However, in this way the files are saved in .txt format and not .dta. If I remove "$out_suffix", and write "geo_`file'.dta, I obtain a .dta file but its name is "geo_`file'.txt.dta". It is not what I need. I need "geo_`file'.dta".

    Many thanks in advance for any suggestions.

    Best,

    Alessandro Giovannini

    ___________________________________
    CEPS Associate Researcher - Economic Policy Unit
    CEPS - Centre for European Policy Studies

  • #2
    You don't say how you have assigned $out_suffix, but what you probably want is:

    Code:
    cd "C:\Users\MyLocalDirectory\"
    local myfilelist : dir . files "*.txt"
    foreach file of local myfilelist {
                  insheet using `file'
                  local outfile = subinstr("`file'",".txt","",.)
                  save "geo_`outfile'", replace
    }
    hth,
    Jeph

    Comment


    • #3
      Many thanks Jeph, problem solved!

      Comment

      Working...
      X