
Title | The correct syntax for post | |
Author | Paul Lin, StataCorp | |
Date | April 1997 |
post myres `val1' `val2'Instead code
post myres (`val1') (`val2')Putting parentheses around the values being posted will solve your problems.
program define mysim ... postfile myres result1 result2 using myfile.dta ... while ... { ... (whatever is a step in your simulation) ... post myres `val1' `val2' } postclose myres endIn this outline, each simulation results in two values being saved. It is inside the while loop that each of the simulations is performed and, at that stage, we are imagining that the two results from a single simulation are produced in local macros 'val1' and 'val2'. The command
post myres `val1' `val2'adds those two values to the dataset being accumulated. Those two results will be an observation in the resulting dataset.
For instance, say that the first time through the loop, 'val1'=3 and 'val2'=7. Say that the second time through, 'val1'=9 and 'val2'=6. Say the the third time through, 'val1'=4 and 'val2'=8. Then the resulting dataset myfile.dta would contain
result1 result2 ----------------------- 3 7 9 6 4 8After running this program, we could see this by typing
. use myfile.dta, clear . list result1 result2 1. 3 7 2. 9 6 3. 4 8and thus we would now be in a position to analyze our simulation results.
Now here is the question our hypothetical user is asking:
The answer has to do with how post interprets lists of expressions. The problem is with the post statement in
while ... { ... (whatever is a step in your simulation) ... post myres `val1' `val2' }Pretend that the first time through the loop, 'val1' contains 5 and 'val2' contains 7. Remember that statements containing macros are interpreted by Stata by simply expanding the macros. Thus,
post myres `val1' `val2'becomes
post myres 5 7and the numbers 5 and 7 are stored for the first observation of result1 and result2. There is no error yet. (post expects to see two values posted because of the earlier post file result1 result2 using myfile.dta statement - - two variables being stored means two values per post.)
The second time through the loop let's pretend the results are 'val1' = 3 and 'val2' = -2. Thus,
post myres `val1' `val2'becomes
post myres 3 -2and you probably now expect the values 3 and -2 will be stored as the second observation. I would however,
post myres 3 -2looks just like
post myres 3-2to Stata, and that looks just like
post myres 1because 3-2=1. Thus, post thinks you specified just one value, not two. post is expecting two values (one for result1, another for result2) and thus, post claims, there is a syntax error!
Here is how we fix it: We change the post command in our loop to read
while ... { ... (whatever is a step in your simulation) ... post myres (`val1') (`val2') }Note the parentheses around the values being posted. Now in the second case when 'val1' = 3 and 'val2' = -2,
post myres (`val1') (`val2')becomes
post myres (3) (-2)and Stata cannot possibly become confused.
So that is why we said, in our short answer, never code
post myres `val1' `val2'but instead code
post myres (`val1') (`val2')Obviously, if you had three results
post myres `val1' `val2' `val3'the recommended coding is
post myres (`val1') (`val2') (`val3')