質問

I am puzzling over this today--it is possible to dynamically set the working directory in Stata using a command in a do-file? For example (on PC at least), you can set the directory in the command line based on the location of the file you are running using:

%cd%

So a command such as the following at the beginning of a batch file will automatically set the directory to the location of where the batch file is saved:

cd %cd%

Which works really well across users. Is there any way to do the same for a do-file (detect/output to local variable the location of where the open do-file is saved on the machine running it)? The idea is to get a smoother version of the following, which detects the proper user path when multiple users are synced to a Dropbox folder and the particular filepath is not the same across users:

#delim ; 
if "`c(username)'"=="Steve" { ; cd "C:/Users/Steve/Documents/My Dropbox/ProjectName/" ; }; 
else if "`c(username)'"=="otherperson" {; cd "/Users/otherpersonusingmac/ProjectName/";};
else if "`c(username)'"=="anotherperson" {; cd "/anotherpersonsDropboxfilepath/ProjectName/";};
# delim cr ;

Thanks!!

役に立ちましたか?

解決

I don't think there's one-step solution, but you could add global macro definitions to the profile.do files on each machine. For example, add the following to your profile.do file.

global DROPBOX "C:/Users/richa_000/Dropbox"
global SKYDRIVE "C:/Users/richa_000/SkyDrive"

Then use these macros to cd in your scripts.

. cd $DROPBOX
C:\Users\richa_000\Dropbox

. cd $SKYDRIVE
C:\Users\richa_000\SkyDrive

Specify the correct path on each machine's profile.do file.

他のヒント

Presumably the do file is located in the current working directory. If so, you can save the creturn value c(pwd) into a local or global macro that you can then call:

global currentdir "c(pwd)'"'

use $currentdir/mydata.dta, clear

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top