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

Re: st: newbie question on replacing variables in do file


From   Philip Ryan <philip.ryan@adelaide.edu.au>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: newbie question on replacing variables in do file
Date   Sun, 15 Feb 2004 17:48:11 +1030

It's sometimes useful in these situations (experimenting with code in a do 
file) to make use of Stata's -tempvar- command.  You can declare a bunch of 
tempvars near the beginning of your sequence of commands, and code using these 
(remembering the left-and-right quote delimiters!), knowing that Stata will 
automatically drop the tempvars at the end of running your commands.

So, as a silly example:

sysuse auto, clear
tempvar var1 var2
gen `var1' = price*2
gen `var2' = !foreign
label var `var2' "US made"
table `var2', c(mean price mean `var1')


once you are sure you have your code correct you may wish to do a search & 
replace with a permanent name (or just maybe elevate the do code to ado
status).


Phil




 Quoting Alan Acock <acock@comcast.net>:

> When I run a series of commands to generate new variables in a do file and
> realize I made a mistake on one of them, I would like to fix the mistake,
> highlight the section of commands, and run the selection. When I do this,
> Stata says the generated variables already exist. Sooooo, I need to drop
> each variable I created in the series of commands, and then re-run them. I
> assume this is a way Stata has to prevent accidently replacing a variable.
> QUESTION: How can I have the gen and egen commands replace the variables I
> want it to replace rathr than having to first drop them???
> Alan Acock
> acock@comcast.net
> 
> *
> *   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/
> 


-- 
Philip Ryan
Associate Professor
Department of Public Health
University of Adelaide
5005 South Australia
AUSTRALIA
CRICOS Provider Number 00123M
----------------------------------------------------------- 
This email message is intended only for the addressee(s) 
and contains information that may be confidential and/or 
copyright.  If you are not the intended recipient please 
notify the sender by reply email and immediately delete 
this email. Use, disclosure or reproduction of this email 
by anyone other than the intended recipient(s) is strictly 
prohibited. No representation is made that this email or 
any attachments are free of viruses. Virus scanning is 
recommended and is the responsibility of the recipient.
*
*   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