Title

wdado -- Create ado file that changes the working directory

General syntax

wdado mnemonic [ , datapath(path-name) replace ]

Overview

wdado makes it simple to have a different working directory for each
project with an optional data location for each project. A simple example using
two projects explains how this works.

Suppose that project AAA keeps its files in the
working directory d:\AAA\work and all datasets are kept in the working
directory.

The working directory for project BBB is
d:\BBB\work and datasets are kept in d:\BBB\data.

To use wdado, change your working directory to d:\AAA\work. Then enter the command:

wdado aaa

An ado file named wdaaa.ado is saved in your
PERSONAL directory. Importantly, PERSONAL is on the adopath, so that any time
you enter the command wdadd Stata will execute wdaaa.ado. For
more information, run help adopath). The ado file includes two commands:

cd "d:\AAA\work"

global S_data "d:\AAA\work"

The first command changes to the working directory for project AAA; the
second command creates a global for where the project keeps datasets. In this
it is the working directory, so the global can be ignored.

Next, change your working directory to d:\BBB\work and and run
wdado bbb, data(D:\BBB\data). File wdbbb.ado is saved in your
PERSONAL directory and includes the commands:

cd "d:\BBB\work"

global S_data "d:\BBB\data"

For either project, I can load a dataset, say mydata.dta, with
the command:

use "$mydata", clear

I use quotes since the data path might include blanks. If the global
S_data is not defined, the command is equivalent to:

use "mydata", clear

which will work as long as the dataset is in the working directory.

Since wdaaa.ado and wdbbb.ado are in PERSONAL, Stata finds them
regardless of what your working directory currently is. For example, I
can run a sequence of commands like this. First, the ado files are created:

. cd "d:\aaa\work"

d:\aaa\work

. wdado aaa

working directory: d:\aaa\work

data path: d:\aaa\work

wdaaa.ado: Saved in D:\Dropbox\Stata15ado\personal/

. cd "d:\bbb\work"

d:\bbb\work

. wdado bbb, data("d:\bbb\data")

working directory: d:\bbb\work

data path: d:\bbb\data

wdbbb.ado: Saved in D:\Dropbox\Stata15ado\personal/

Now I can use the new commands to change working directories:

. wdaaa

d:\aaa\work

. pwd

d:\aaa\work

. display "S_data: $S_data"

S_data: d:\aaa\work\

. wdbbb

d:\bbb\work

. pwd

d:\bbb\work

. display "S_data: $S_data"

S_data: d:\bbb\data\

Options

datapath(path-name) is the path where data is stored. The
path is stored in the global S_data. Since the name begins with
S_ it is not removed when you run clear all or
macro drop _all.

replace if ado file exists, replace it.

Examples

Citing wdado.ado

When using these commands, please cite:

J. Scott Long. 2017.
Ado files for managing project directories in Stata.

http://www.indiana.edu/~jslsoc/