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: How to write byable ado file that adds variables to data |
Date | Wed, 23 Nov 2011 22:59:51 +1030 |
One option is to check whether or not the variable already exists, using that to determine whether to -gen- or -replace- the variable. For example: capture confirm new variable newvar if !_rc { gen newvar=... if `touse' } else replace newvar=... if `touse' Phil On 23/11/2011, at 10:49 PM, Richard Herron wrote: > It appears that my first program doesn't work without -by-, either (I > must have had the original program cashed). Here is the program that > works without -by-. Thanks! > > *! price_fun v0.1 Richard Herron 11/21/2011 > program price_fun > version 11.2 > args price eps1 eps2 eps3 eps4 eps5 beta bkvlps rf r g > tempvar ri num den > generate `ri' = `rf' + `beta'*`r' > generate price_hat_`rf'_`r'_`g' = `bkvlps' /// > + `eps1'/(1 + `ri'/100) /// > + `eps2'/(1 + `ri'/100)^2 /// > + `eps3'/(1 + `ri'/100)^3 /// > + `eps4'/(1 + `ri'/100)^4 /// > + `eps5'/(1 + `ri'/100)^5 /// > + (1 + `g'/100)*`eps5'/(`ri'/100 - `g'/100)/(1 + `ri'/100)^5 > generate `num' = abs(price_hat_`rf'_`r'_`g' - `price') > egen `den' = rowmin(price_hat_`rf'_`r'_`g' `price') > generate w_`rf'_`r'_`g' = `num'/`den' > end > > On Wed, Nov 23, 2011 at 07:03, Richard Herron > <richard.c.herron@gmail.com> wrote: >> I am writing my first byable program. The program works without -by-, >> but with -by- it fails on the second subset with the error "_variable_ >> already defined." This is logical because I am adding two variables to >> my data, but I am not sure of the work-around. Is it not possible to >> add variables in a byable program? Thanks! >> >> Here is the program. >> >> *! price_fun v0.1 Richard Herron 11/21/2011 >> program price_fun, byable(recall) >> version 11.2 >> syntax varlist(min=11 max=11 numeric) [if] [in] >> marksample touse >> tokenize `varlist' >> local price `1' >> local eps1 `2' >> local eps2 `3' >> local eps3 `4' >> local eps4 `5' >> local eps5 `6' >> local beta `7' >> local bkvlps `8' >> local rf `9' >> local r `10' >> local g `11' >> tempvar ri num den >> generate `ri' = `rf' + `beta'*`r' if `touse' >> generate price_hat_`rf'_`r'_`g' = `bkvlps' /// >> + `eps1'/(1 + `ri'/100) /// >> + `eps2'/(1 + `ri'/100)^2 /// >> + `eps3'/(1 + `ri'/100)^3 /// >> + `eps4'/(1 + `ri'/100)^4 /// >> + `eps5'/(1 + `ri'/100)^5 /// >> + (1 + `g'/100)*`eps5'/(`ri'/100 - `g'/100)/(1 + `ri'/100)^5 /// >> if `touse' >> generate `num' = abs(price_hat_`rf'_`r'_`g' - `price') if `touse' >> egen `den' = rowmin(price_hat_`rf'_`r'_`g' `price') if `touse' >> generate w_`rf'_`r'_`g' = `num'/`den' if `touse' >> end >> * >> * 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/