Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: preserve & restore


From   "Jun Xu" <mystata@hotmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: preserve & restore
Date   Sat, 22 Feb 2003 12:02:09 -0600

Stata Listers:

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*/

end

trial
*********************************************************


Thanks a lot!!!

Jun Xu
Department of Sociology
Indiana University at Bloomington


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* http://join.msn.com/?page=features/virus

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index