Dear statalist,
I have a big file that overwhelms my memory when I try to run my script.
Therefore, I wish to break the operation down into chunks, using postfile to write the results of each loop iteration into a new .dta file.
This way, I hope to be able to check in on my progress while the loop is running, by opening the resulting file from time to time.
I mostly want to generate and modify variables according to simple decision rules, and then visualise some of the results.
However, as I tried making a minimal example using a smaller subfile, I noticed the following error: the resulting postfile only contains the results of the first loop iteration.
The loop itself works, and generates exactly what I want to have generated.
Setting -trace- I found that the loop indeed only processed the "post" command for the first iteration, and from then on only reads it without doing anything.
If I put the -post- command into the outer loop, it repeats one observation, but does surprisingly *not* overwrite each iteration until the last one, but again takes the result of the first iteration.
The results of running the code below are:
iteration, category, year, mean_difference \\
1, $Some_CategoryA$, 1930, -0.1
The results of putting the post-command into the outer loop are:
iteration, category, year, mean_difference \\
1, $Some_CategoryA$, 1930, -0.1\\
2, $Some_CategoryA$, 1930, -0.1\\
....
....
....
47 $Some_CategoryA$, 1930, -0.1\\
See my code below. For confidentiality reasons, I am sorry cannot share the actual code and data with you. Thank you in advance!!
gen x = " "
gen mean_difference = 888
capture postutil clear
tempname buffer
postfile `buffer' iteration str33 category year mean_difference using "differenceA1.dta", replace
set trace on
egen group = group(category)
su group, meanonly
forval i = 1/`r(max)' {
if group == `i'{
replace x = "yes" if $some_rule$
keep if x == "yes"
egen m_y1 = mean(y1), by (category year)
egen m_y2= mean(y2), by (category year)
replace mean_difference = m_y1 - m_y2
post `buffer' (`i') (category) (year) (mean_difference)
drop m_y1 m_y2
}
}
postclose `buffer'
Related Posts with Postfile only posts results of first iteration of the loop
How to set width Stata MP 16.1 for UnixHello, I am using Stata 16.1 (64-bit, total usable memory 1510.3 GB), and trying to change an extre…
Panel data with messy time, very large NDear users, My data consists of N=1033 and T=1-15, with a total number of observation of 2300. Time…
Regressing Firm-specific variables against Macroeconomic variablesHello, I am using panel data with the following observations: The firm-specific dependent variable…
Reshaping from "strange" to long formatDear StataListers, I use Stata 14.2. Here is a simplified example of my data (three cities, three y…
Avoiding merge m:m problem...Dear Statalisters: I have the following file #1: clear input byte(firmid ownerid) float own byte l…
Subscribe to:
Post Comments (Atom)
0 Response to Postfile only posts results of first iteration of the loop
Post a Comment