Hello Statalist Community,
I hope everything is well!
I'm going crazy with a loop I'm trying to implement on stata. I would like to save the results to another .dta file, as my dataset is very large, and to do this I use the -post- command.
However, I've been getting the same error for a while, and I don't understand why it's happening.
Here's part of my do-file:
And this is the error I get:
I think it's a coding error on my part, but I don't know where. Here is a small -dataex-:
I beg you: could someone please help me understand my error?
Many thanks in advance!
Michael
I hope everything is well!
I'm going crazy with a loop I'm trying to implement on stata. I would like to save the results to another .dta file, as my dataset is very large, and to do this I use the -post- command.
However, I've been getting the same error for a while, and I don't understand why it's happening.
Here's part of my do-file:
Code:
// --------------------------------------------- //
clear all
capture log close
set more off
set seed 202402
set varabbrev off
version 17
// --------------------------------------------- //
* global
global data "C:/Users/miduarte/Desktop/Ongoing_Projects/test_HolaLuz_Data/New_dataset_15112023/stata/2_export_telemedida/data/random/"
global destkop "C:/Users/miduarte/Desktop"
// --------------------------------------------- //
use `id_zip_complete', clear
cd "${desktop}"
*Set up file to capture coefficient estimates
postutil clear
tempname iLevOLSResults
postfile `iLevOLSResults' ID p_ols0 se_ols0 p_ols1 se_ols1 p_ols2 se_ols2 p_ols3 se_ols3 ///
using "iLevOLSResultsIV_GN.dta", replace
levelsof id, local(idlist)
local loop = 1
disp c(current_time)
foreach i of local idlist {
disp "loop == `loop'"
disp "id == `i'"
preserve
keep if id == `i'
qui {
*generate time and temp variables
egen byte ym = group(y m)
gen int block=1 if hr>=1&hr<=4
replace block=2 if hr>=5&hr<=8
replace block=3 if hr>=9&hr<=12
replace block=4 if hr>=13&hr<=16
replace block=5 if hr>=17&hr<=20
replace block=6 if hr>=21&hr<=24
gen lnkwh = asinh(kwh)
gen int peak = 1 if block >= 3
replace peak = 0 if peak == .
foreach x in 1 7 365 {
gen tau_`x' = (t+0.5)/`x'*_pi*2
gen tau2_`x' = tau_`x'^2
forvalues k = 1(1)4 {
gen cos`k'tau_`x' = cos(tau_`x'/`k')
}
}
* re-scale variables
foreach v of varlist tau* {
egen mean`v' = mean(`v')
egen sd`v' = sd(`v')
replace `v' = (`v'-mean`v')/sd`v'
drop mean`v' sd`v'
}
}
* Regressions *****************
* Create locals (clean from previous loop)
forvalues spec = 0(1)3 {
cap forvalues inst = 0(1)1 {
local beta_ols`spec'`inst' = .
local beta_se`spec'`inst' = .
}
}
* Fixed effects
forvalues spec = 0(1)3 {
cap {
qui reghdfe lnkwh, absorb(`xvars_ols`spec'')
local beta_ols`spec' = e(b)[1,1]
local se_ols`spec' = sqrt(e(V)[1,1])
}
cap forvalues inst = 0(1)1 {
qui ivreghdfe lnkwh, absorb(`xvars_ols`spec'')
local beta_iv`spec'`inst' = e(b)[1,1]
local se_iv`spec'`inst' = sqrt(e(V)[1,1])
}
}
post `iLevOLSResults' (`i') (`beta_ols0') (`se_ols0') ///
(`beta_ols1') (`se_ols1') ///
(`beta_ols2') (`se_ols2') ///
(`beta_ols3') (`se_ols3')
restore
local loop = `loop' + 1
}
postclose `iLevOLSResults'
use "${desktop}/iLevOLSResultsIV_GN.dta", clear
exit, clear
Code:
invalid syntax post: above message corresponds to expression 4, variable p_ols1 r(198);
I think it's a coding error on my part, but I don't know where. Here is a small -dataex-:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long id float(y m dom) byte hr float kwh long sp_zipcode 1039 2021 3 24 18 .219 8021 1039 2021 2 10 12 .146 8021 1039 2021 1 31 3 .133 8021 end
I beg you: could someone please help me understand my error?
Many thanks in advance!
Michael

Comment