Announcement

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

  • Counting files in a directory: why is there always one too many?

    Dear Statatlist,

    I am using Stata 13 on a Windows 10 PC and have a number of data sets in a folder that I would like to append:

    Code:
    cd "prepared data"
    
    local filelist : dir . files "*.dta"  // Create local with all filenames ending with ".dta"
    di `filelist'
    
    local first : word 1 of `filelist'      // Identify first file
    di "`first'"
    
    local total_ : word count of `filelist' // Indentify total number of files
    di `total_'        
    
    use "`first'", clear
    forvalues x = 2/`total_' {
        di "`x'"
        local y : word `x' of `filelist'
        append using "`y'"
    }
    However, this doesn't work -- the word count of `filelist' is always one too many. Is there a reason for this?

    Thanks for your consideration
    Go

  • #2
    Remove the "of" in your word count command. Stata is reading this as an extra string:

    Code:
    local total_ : word count  `filelist'
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Thank you so much, I would have never figured that out.

      Best regards
      Gobinda

      Comment

      Working...
      X