문제

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