Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: st: AW: Keeping variable After restore


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: AW: Keeping variable After restore
Date   Mon, 9 Nov 2009 15:46:17 +0100

<> 


" you can create a new dataset out of the preserved data & -merge- it"


If you want to follow Eric`s advice, consider a -tempfile-:

***
clear
sysuse auto
expand 2
gen year = 2000 in 1/50
replace year = 2001 in 50/l
sort make year
**
preserve
keep if year == 2000
factor mpg price rep78
predict var1
keep make year var1
sort make year
tempfile formerge
save `formerge', replace
restore

merge make year using `formerge'
browse make year var1
***



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Eric Booth
Gesendet: Montag, 9. November 2009 15:42
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: AW: Keeping variable After restore 

Or if you insist on using -preserve-/-restore-, you can create a new  
dataset out of the preserved data & -merge- it (though I think  
Martin's solution using conditionals is better, especially if your  
dataset is really large--saving a new dataset and merging it back into  
the master would be slower):

*****
clear
sysuse auto
expand 2
gen year = 2000 in 1/50
replace year = 2001 in 50/l
sort make year
**
preserve
keep if year == 2000
factor mpg price rep78
predict var1
keep make year var1
sort make year
save formerge.dta, replace
restore

merge make year using formerge.dta
browse make year var1
*****

Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754


On Nov 9, 2009, at 8:28 AM, Martin Weiss wrote:

>
> <>
>
> Instead of -keep-ing the year 2000:
>
> ***
> webuse bg2, clear
> gen year=2000*(_n<200)+ /*
> */ 2001*(_n>=200)
> qui factor bg2cost1-bg2cost6  /*
> */ if year==2000
> predict var1
> ***
>
> and off you go...
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von C.T.  
> Shehzad
> Gesendet: Montag, 9. November 2009 15:22
> An: statalist@hsphsun2.harvard.edu
> Betreff: st: Keeping variable After restore
>
> Dear Stata-listers:
>
> I want to generate a variable during (preserve - restore) and want to
> use that after restoring the data. When I will restore data the  
> variable
> will be dropped automatically. How can I keep the variable?
>
> The data is time series and a simple version of my program is as  
> follows:
>
> preserve
> keep if year == 2000
> factor x1 x2 x3
> predict var1
> restore
>
> gsort -year
> replace var1= var1[_n-1] if var1 >= .
> xtset country year
> xtreg dep var1 controls
>
> The problem in this program is that after restoring the data I will  
> not
> have var1 anymore. What can be a possible solution?
>
> Thanks and best regards,
>
> Tanveer
>
>
>
> *
> *   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/



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