Announcement

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

  • Import delimited with global loop error

    I have a list of over a thousand .csv files named 132086 145087 156086 etc (without a clear pattern in the names)

    The file path is "...\data\individuals" and the .csv files are inside the "individuals" folder (to clarify, the "..." is just to not write down my specific path but I include the full path in the command).

    I tried importing them as follows:

    Code:
    global individuals 132086 145087 156086 etc
    
    foreach ind in $individuals{
    
    import delimited "...\data\individuals\`ind'.csv", clear
    
    save imported_`ind', replace
    
    }
    However I get an error that says "file ...\data\individuals.csv not found"

    However, when I use the same command without the loop (e.g. "import delimited "...\data\individuals\132086.csv", clear") it does work.

    Could anyone help me find what I am doing wrong?

    Thank you!



  • #2
    I think the syntax is not right.
    Code:
    foreach ind of global individuals {
    }

    Comment


    • #3
      This is a common issue that comes up from time to time. Use a double backslash or a forward slash. Also, don't use globals if you don't have to. If you search through the forum, you will find the dangers of using globals discussed.


      Code:
      global individuals 132086 145087 156086 etc
      
      foreach ind in $individuals{
      
      import delimited "...\data\individuals\\`ind'.csv", clear
      
      save imported_`ind', replace
      
      }
      or

      Code:
      global individuals 132086 145087 156086 etc
      
      foreach ind in $individuals{
      
      import delimited "...\data\individuals/`ind'.csv", clear
      
      save imported_`ind', replace
      
      }
      Last edited by Andrew Musau; 06 May 2022, 08:55.

      Comment


      • #4
        Thank you very much!

        Comment

        Working...
        X