Monday, March 06, 2006

st: expanding a macro

Robert wrote

I am still having trouble using a macro to refer to a windows directory: Explicitly naming the directory & file in a use statement works works. E.g.,

> use F:\0_stat.data\0_homenet.data\0_freshmen\transition\withlag_2sls.dta, clear

I can define the macro, and display seems to indicate that my definition is correct:

> local dir F:\0_stat.data\0_homenet.data\0_freshmen\transition> di "`dir'""withlag_2sls.dta" > F:\0_stat.data\0_homenet.data\0_freshmen\transition\withlag_2sls.dta

However, I still get an error when I add the file name, either surrounding the filename in quotes or not:

> use " `dir'""withlag_2sls.dta", clear > invalid '"withlag_2sls.dta' > r(198); > > use " `dir'"withlag_2sls.dta, clear > invalid 'withlag_2sls.dta' > r(198);

Defining the macro using quotes around the directory names does not seem to help. Again, the macro seems to expand correctly using the display command, but not with the use command.

> local dir "F:\0_stat.data\0_homenet.data\0_freshmen\transition\"

> . di "`dir'""withlag_2sls.dta" > F:\0_stat.data\0_homenet.data\0_freshmen\transition\withlag_2sls.dta >

> . di "`dir'""withlag_2sls.dta" > F:\0_stat.data\0_homenet.data\0_freshmen\transition\withlag_2sls.dta > > . use "`dir'""withlag_2sls.dta", clear > invalid '"withlag_2sls.dta' > r(198); >

Any suggestions? I must not be understanding something fundamental about expanding macros.

This is a FAQ. Ditch the backslashes (which are taken by Stata to suppress macro expansion) in favor of forward slashes. (To provide greater user satisfaction, ditch operating systems which make use of backslashes, but that is another issue). Stata will cope perfectly well with C;/directory/directory/filename.

Kit Baum, Boston College Economics http://ideas.repec.org/e/pba1.html * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/


Tag:


Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?