Question about multiple perserve and restore in an ado file. Here Stata
will produce error message since I have two preserve's in the following ado
file. As I need to create a data set that is a subset of the one managed
within the second "preserve", I use preserve and restore since I am going to
work on the one immediately before the second preserve. But I still need to
put preserve right below program define command since there are some other
manipulations outside the second preserve/restore block like the sort
command. One way that's possible is to undo some data management witin this
ado like sort back to the original data before executing this ado file as I
am doing at teh end of the ado file. But are there any other tricks to go
about it?
****************************************************
for num 1/6: set obs 100\ gen xX=invnorm(uniform())
capture program drop trial
program define trial
perserve /*first perserve*/
tempvar obsn what
quietly gen `obsn'=_n
***Do other stuff that changes the original data look
sort x4 /*such as sort by x4*/
preserve /*second preserve*/
gen `what'=101-_n
sort `what'
***
***do other stuff here based on the sorted order
***such as just keep the top 10 values of `what'
drop if x3==.
save h:\trial2.dta, replace
restore /*second restore*/
*do something else here that might change the order
*or look of the original data
sort `obsn'
restore /*corresponding to the first preserve, though not necessary*/