Announcement

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

  • problema with local extended_fcn local list : dir . files "*"

    Hello everyone.

    this is my first post in Statalist, so please forgive any mistake on it.

    I'm trying to capture the filenames of several files in any given directory using:

    local filename : dir . files "*"

    This actually works and it creates a local named `filename' containing the names of every file in the directory. The problem is that it does not separates the filenames from eachother, so I get a string: fosis.xlsxjunaeb.xlsxmatriz2019.xlsxmineduc.xlsxse nama.xlsxsename.xlsxsence.xlsxsernameg.xlsxssmds.x lsxsstrabajo.xlsx (names are arbitrary)

    Thus, I am unable to work with each file separatedly as I would like. Does anyone knows how to solve this?

    I appreciate your answers.

    **Im currently working with STATA 14 running in Windows 10 in a Lenovo Thinkpad 51 signature edition**
    Last edited by Camilo Araneda; 27 Feb 2019, 10:43.

  • #2
    It does separate them. The question is what you are trying to do with local filename?

    This is what I get in one directory (omitting output irrelevant to the question)

    Code:
    . local filenames : dir . files "*"
    
    . mac li
    _filenames:     "auto.dta" "isstata.150" "profile.do"  "stata.html" "stata.lic" "statase-64.exe" "statase-64.exe - shortcut.lnk" "statase-64_old.exe"
    I can loop over those with something like

    Code:
    foreach file of local filenames { 
          di `"`file'"'
    }

    Comment


    • #3
      Nick has explained the problem, I will point to alternatives. Camilo might want to check out filelist(SSC, Picard) that searches directories recursively. Given that most file names are Excel files, Camilo might also want to check my xls2dta (SSC), which converts and combines Excel files.

      Best
      Daniel

      Comment


      • #4
        Thank you, Nick and Daniel.
        I came to realize it actually separates them. I don't understand why it would give me an error while performing:

        foreach file of local file {
        clear
        import excel using `file', first case(l) allstring
        if `file' != "matriz2019" {
        append using "matrizinclusion.dta"
        }
        save "matrizinclusion.dta", replace
        }

        I think it had to do with double quotes ("). Now I have to deal with gettin the loop correctly specified to achieve my goal of importing and then appending multiple datasets.

        Thank you very much for your time,

        best.
        Camilo

        Comment


        • #5
          I wouldn't use the same name in two contexts, even if Stata lets you do it. Also, I think there is at least one bug lurking inside that loop.

          Code:
          foreach f of local file { 
              clear
              import excel using "`f'", first case(l) allstring
              if "`f'"  != "matriz2019.xls" {
                  append using "matrizinclusion.dta"
              }
              save "matrizinclusion.dta", replace
          }
          Please note the use of (1) CODE delimiters (2) indenting to make code more readable.

          Comment

          Working...
          X