Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paths to do-file in Stata

Tags:

stata

Would it be possible, when I launch a do-file to recover the path of the do-file as a variable?

I'm sharing a project with a co-author (via Dropbox). The structure of the folders (data, logs, etc.) is therefore the same on both sides.

But the folders are situated differented on my coauthor's filesystem and mine. It would therefore be helpful for us to write do-file that are agnostic about the path of the folders, etc.

We would like the path to our projects not to be hard-coded.

Thank you!

like image 984
Peutch Avatar asked Feb 12 '13 14:02

Peutch


2 Answers

You can do various things in this territory, including

  • Passing the name of a directory to a do-file as an argument. For example,

    do mydo d:/myproject/data1812 
    

    launches the file mydo.do and passes the argument of a particular directory to the do-file. Inside the do-file you can grab the argument as

    local myfolder "`1'" 
    

    i.e. the thing passed is passed as local macro 1. (Any other arguments would be local macros 2, 3, etc.)

  • Make sure your references to locations generally and files in particular are relative within the do-file and run the do-file from the parent directory.

  • Use global macros within your main do-file for locations and then re-define them within a master do-file which you run first.

Notes: It's best to use forward slashes, even under Windows; Stata will translate. Also, if there are embedded spaces, bind the whole thing in double quotes.

  do mydo "d:/my project/data1812" 

The second seems closest to your preference for not wiring in particular locations. But if you are using files from different places you have to tell Stata somehow where they are....

like image 143
Nick Cox Avatar answered Sep 21 '22 14:09

Nick Cox


Nick's comment above gives the answer: c(pwd). This gives you a relative starting point for later commands, e.g. opening a dataset in the data folder:

use `c(pwd)'/data/yourdata, clear

Your problem might be that double-clicking a do-file does not cause Stata to set the working directory to its folder (while it does for datasets, which is inconsistent and not necessarily helpful).

There is no particular solution to that issue, except perhaps by writing your project folder path into a global macro set at startup by your profile.do file in your Stata application folder.

I teach classes of students and have them set their working directory with such a system. It works alright.

like image 23
Fr. Avatar answered Sep 23 '22 14:09

Fr.