Announcement

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

  • Migrate from Mac to PC, problems with foreach loop

    Hi all-
    I am having issues running a loop to import .csv files. (I have many files to import, I just include one file below for simplicity.)

    The following code works successfully on STATA15:
    clear all
    insheet using "C:\Users\Jen\Documents\Research\CorporateBoards_n ew\Raw_data\Staticdata\deadbd1.csv"

    However, when I update it to a loop (code below), it does not work:
    clear all
    foreach file in deadbd1 {
    insheet using "C:\Users\Jen\Documents\Research\CorporateBoards_n ew\Raw_data\Staticdata\`file'.csv"
    }

    It gives me the following error:
    file
    C:\Users\Jen\Documents\Research\CorporateBoards_ne w\Raw_data\Static
    > data`file'.csv not found
    r(601);

    I just moved all my stata-related files over from my old macbook to my new windows pc (and updated the working directory paths, etc.). This is the first code that I am attempting to run on the new windows computer, and it is not working. I am very confused because this is a super simple code that has worked for me many times on my old macbook. Has anyone else run into this issue when migrating from mac to pc?

    Thanks for your help.

  • #2
    Welcome to Statalist.

    The backslash character
    Code:
    \
    has meaning to Stata: it will prevent the interpretation of any following character that has a special meaning to Stata, in particular
    Code:
    `
    will not be interpreted as indicating a reference to a macro.

    But all is not lost: Stata will allow you to use the forward slash character in path names on any operating system, and on Windows will take care of doing what must be done to appease Windows.

    Code:
    . local prefix batch42
    
    . display "Q:\HOME\`prefix'_filename"
    Q:\HOME`prefix'_filename
    
    . display "Q:/HOME/`prefix'_filename"
    Q:/HOME/batch42_filename

    Comment


    • #3
      I wonder if CorporateBoards_n ew should be CorporateBoards_new.
      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        William - I was previously unaware that a single backslash had special meaning to Stata. That resolved the issue!
        Bruce - Good catch on the random space. I double-checked my folder names and removed unnecessary spaces to prevent confusion.
        Thank you both so much!

        Comment

        Working...
        X