0
votes

I am running into a strange problem while trying to run my code. The following is a sample of my code (Note: I am already in the main directory):

cd "C:/`mydir'" //where `mydir' is the local for the folder containing the fig folder.

use cleaned.dta, clear

local outcome drr_trtd 

tempname post_handle

local postfile_varlist str12(tau_text) double(pt_est se) 

postfile `post_handle' `postfile_varlist' using "fig/dta/es_M12_P11_`outcome'.dta", replace

postclose `post_handle'

use "fig/dta/es_M12_P11_`outcome'.dta", clear

The issue is that whenever I try and run this code, I get an error:

postfile `post_handle' `postfile_varlist' using "fig/dta/es_M12_P11_`outcome'.dta", replace
fig/dta/es_M12_P11_drr_trtd.dta invalid name

I assumed that this may be because of quotation marks, so I removed the quotation marks and then I get the following error:

postfile `post_handle' `postfile_varlist' using fig/dta/es_M12_P11_`outcome'.dta, replace
/ invalid name

I then tried to make the file without a file-path that is I edited the postfile command as follows:

postfile `post_handle' `postfile_varlist' using "hello.dta", replace

Now, with the quotations I get the error:

"hello.dta invalid name

Without the quotation I get the same error but with hello.dat. Am I just doing something obviously wrong here? I do not understand why I am getting this error.

New Code:

local mydir /Users/F H/Desktop/Ray/analysis
cd "C:/`mydir'/" 

tempname post_handle
local postfile_varlist str12(tau_text) double(pt_est se) 
pwd

postfile `post_handle' `postfile_varlist' ///
    using "\fig\dta\event_study_M24_P59_sa.dta" ///
    , replace

I get the error:

(note: file \fig\dta\event_study_M24_P59_sa.dta not found)
file \fig\dta\event_study_M24_P59_sa.dta could not be opened
2
What do you mean when you say "I am already in the main directory". Does that mean you're in the fig directory? Try posting a reproducible example showing your error. Something like I've posted below. (I can't reproduce it.) - Roberto Ferrer
Are you using an updated version of Stata? Run update. - Roberto Ferrer
Are you running your code in parts? Run it all at once and report back. - Roberto Ferrer
Insert macro list just before the postfile call. That will show you which local macros are visible. The local macro var is not defined in your code, although that doesn't seem important here. - Nick Cox
I can't follow the details, but I still conjecture that Stata can't figure out where you want the output file compared with where you are working. I generally work in exactly the same directory as I want to save stuff. Naturally there are exceptions, but whereas I sometimes get confused, I have not known Stata to be wrong here. - Nick Cox

2 Answers

2
votes

I can't seem to reproduce your error. A working example:

sysuse auto, clear

local outcome drr_trtd 

tempname post_handle

local postfile_varlist str12(tau_text) double(my_price myrep)

postutil clear

postfile `post_handle' `postfile_varlist' ///
    using "test/test2/resu_lts_`outcome'.dta", replace

post `post_handle' (make[1]) (price[1]) (rep78[1])

postclose `post_handle'

use "test/test2/resu_lts_`outcome'.dta", clear

list

(I've created the appropriate testing directories.)

1
votes

The following test works for me so long as I have write permission in the current directory, and not otherwise.

sysuse auto, clear
local outcome drr_trtd 
tempname post_handle
local postfile_varlist str12(tau_text) double(my_price myrep)
postutil clear
postfile `post_handle' `postfile_varlist' using `outcome'.dta, replace
post `post_handle' (make[1]) (price[1]) (rep78[1])
postclose `post_handle'
use `outcome'.dta, clear
list