Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Phil Clayton <philclayton@internode.on.net> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: use tempfile |
Date | Tue, 19 Jun 2012 23:46:44 +0930 |
This works for me: /*===begin===*/ sysuse auto, clear local arg "yes" preserve tempfile tempresults gen X = 1 save "`tempresults'" if "`arg'" == "no" { restore, preserve } else { restore, not use `tempresults', clear } /*===end===*/ I am left with a dataset containing the variable X - this is what was saved to `tempresults'. If this is not happening for you then most probably you haven't shown us enough of your code (eg you have another -preserve- buried somewhere). Of course, the above code is rather messy - this would achieve the same end: /*===begin===*/ sysuse auto, clear local arg "yes" if "`arg'"=="yes" { gen X = 1 } /*===end===*/ Presumably you're trying to do something more complicated. But I suspect that in this case the devil is in the details. Phil On 19/06/2012, at 11:37 PM, Lythgoe, Dan wrote: > Maarten, Phil thanks for the input so far. Phil your suggested code makes perfect sense to me but still doesn't seem to quite do the job; I want it to clear the current data set and use the `tempresults' data set instead. > > Dan > > > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Phil Clayton > Sent: 19 June 2012 14:41 > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: use tempfile > > If you -preserve- within a do-file (or program), Stata will automatically -restore- at the end of the do-file unless you specifically tell it not to. > > Try something like this: > > /*===begin===*/ > local arg "yes" > > preserve > tempfile tempresults > gen X = 1 > save "`tempresults'" > > if "`arg'" == "no" { > restore, preserve > } > else { > restore, not > use `tempresults', clear > } > /*===end===*/ > > Phil > > On 19/06/2012, at 10:54 PM, Lythgoe, Dan wrote: > >> Dear Statalist, >> >> Apologies in advance for such a simple question but I am struggling with this one. I want to -use- a temporary file stored using -tempfile- if an argument is true(/false), but am clearly missing something as I can't seem to get STATA to -use- the temporary file: >> >> >> /*===begin===*/ >> local arg "yes" >> >> preserve >> tempfile tempresults >> gen X = 1 >> save "`tempresults'" >> >> if "`arg'" == "no" { >> restore, preserve >> } >> else { >> use `tempresults', clear >> } >> /*===end===*/ >> >> Any assistance would be greatly appreciated. >> >> Many thanks >> Dan Lythgoe >> >> >> >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/statalist/faq >> * http://www.ats.ucla.edu/stat/stata/ > > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/