Announcement

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

  • Issues with running stata in batch mode

    Hi all,

    Recently I try to streamline my data analysis by using the windows batch file to call do files. However, I find something strange about setting the current working directory, and overwriting generated files. For example, I have a do file named auto.do, whose only purpose is to download the auto data and save it; it looks like this:
    Code:
    sysuse auto, clear
    save auto.dta
    And it's located in D:\Projects\p1.

    I also write a cmd file calling the do file, and the cmd file looks like this:
    Code:
    cd /d D:\Projects\p1
    Stata /e /q do "D:\Projects\p1\auto.do"
    Notice that I set the current working directory to the p1 folder, while my default Stata working directory is not this one.

    This completes the setup. And Here are the questions:

    1) It doesn't seem that the working directory I set in the cmd file is respected by Stata. Stata saves the auto data still to the default stata working directory, not the p1 folder. Also if I don't give the full path of the do file, Stata complains that it doesn't find the do file.

    2) Stata saves the auto data to its default working directory. However, if I run the batch file again, the previously generated auto data is automatically overwritten, with no warning or any other message poped up. Also notice that I don't include the replace option in the save command in the do file. How could this happen? And what will be a good strategy to deal with this uninformed overwriting.

    3) A log file is generated with /e specified. The issue is that this log file is indeed saved in the p1 folder, which I set as the working directory in the 1st line of the cmd file. This might not be a big issue for me since I prefer to include the log command in the do file, and not to use /e in the first place. But just curious to know why this phenomenon happens.

    To conclude, the setting of working directory in the cmd file is ignored by Stata when new files are read or written in the do file. But the log file does respect the working directory set int he cmd.

    Thanks!

  • #2
    As described here the log file will be saved in the working directory of your command prompt (Method 2). Since you change the working directory to D:\Projects\p1 the log file will be saved here. Now when lauching Stata, it has no knowledge of the working directory set from the command prompt and as such the save command will save the file in Stata's default directory. If you want to save the file in D:\Projects\p1 you have to tell Stata in your do-file


    Code:
    pwd // it will show you the working directory when Stata starts
    cd D:\Projects\p1
    sysuse auto, clear
    save auto.dta

    Comment


    • #3
      Originally posted by Christophe Kolodziejczyk View Post
      As described here the log file will be saved in the working directory of your command prompt (Method 2). Since you change the working directory to D:\Projects\p1 the log file will be saved here. Now when lauching Stata, it has no knowledge of the working directory set from the command prompt and as such the save command will save the file in Stata's default directory. If you want to save the file in D:\Projects\p1 you have to tell Stata in your do-file


      Code:
      pwd // it will show you the working directory when Stata starts
      cd D:\Projects\p1
      sysuse auto, clear
      save auto.dta
      I see. So the working directory set in the command line is not necessarily the working directory of Stata.

      Comment

      Working...
      X