Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Gerard Solbrig" <gsolbrig@mail.uni-mannheim.de> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: Looping within a subset under a certain condition |
Date | Mon, 1 Oct 2012 18:42:40 +0200 |
Now, the code works the way it is supposed to work! I find the intuition behind the code very appealing! Can't thank you enough for the assistance. Especially you, Nick. Being new to Stata, I learned a lot from this thread and the correspondence. Maybe I'll be able to make valuable contributions soon, too. Best, Gerard -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Montag, 1. Oktober 2012 02:38 To: statalist@hsphsun2.harvard.edu Subject: Re: st: Looping within a subset under a certain condition So, straight away the first firm has no cases with rep == 1. Nothing doing in those circumstances. Also, you messed with the rest of my code without explaining why. I recommend as follows. You need to be consistent on -date- and -trandate-. sort cusip6 rep date gen long obs = _n gen rep_ins = 0 egen firm_numid = group(cusip6) summarize firm_numid, meanonly forvalues x = 1/`r(max)' { su obs if firm_numid == `x' & rep == 0, meanonly local z1 = r(min) local z2 = r(max) su obs if firm_numid == `x' & rep == 1, meanonly local o1 = r(min) local o2 = r(max) if missing(`z1', `z2', `o1', `o2') continue forvalues i = `z1'/`z2' { local isin = 0 forvalues o = `o1'/`o2' { if inrange(trandate[`i'], wind_start[`o'], wind_end[`o']) { local isin = 1 } } if `isin' == 1 replace rep_ins = 1 in `i' } } On Sun, Sep 30, 2012 at 9:15 PM, Gerard Solbrig <gsolbrig@mail.uni-mannheim.de> wrote: > Here's what Stata says: > > - forvalues x = 1/`r(max)' { > = forvalues x = 1/18554 { > - summarize obs if firm_numid == `x' & rep == 0, meanonly = summarize > obs if firm_numid == 1 & rep == 0, meanonly > - local z1 = r(min) > - local z2 = r(max) > - summarize obs if firm_numid == `x' & rep == 1, meanonly = summarize > obs if firm_numid == 1 & rep == 1, meanonly > - local o1 = r(min) > - local o2 = r(max) > - forvalues i = `z1'/`z2' { > = forvalues i = 1/106 { > - local isin = 1 > - forvalues o = `o1'/`o2' { > = forvalues o = ./. { > invalid syntax > if inrange(trandate[`i'], wind_start[`o'], wind_end[`o']) { > local isin = 0 > } > if `isin' == 1 replace rep_ins = 1 in `i' > } > } > r(198); > > > > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu > [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox > Sent: Sonntag, 30. September 2012 21:53 > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: Looping within a subset under a certain condition > > This code refers to -date- and -trandate- at different places. > > gen long obs = _n > > was recommended earlier. > > Type > > set trace on > set tracedepth 1 > > before running the code and see which line produces the error. > > On Sun, Sep 30, 2012 at 7:28 PM, Gerard Solbrig > <gsolbrig@mail.uni-mannheim.de> wrote: >> I'm sorry, but I've been trying for hours now: Stata yields me >> "invalid syntax r(198);" every time I try to run this code: >> >> sort cusip6 rep date >> gen obs = _n >> gen rep_ins = 0 >> egen firm_numid = group(cusip6) >> summarize firm_numid, meanonly >> forvalues x = 1/`r(max)' { >> su obs if firm_numid == `x' & rep == 0, meanonly >> local z1 = r(min) >> local z2 = r(max) >> su obs if firm_numid == `x' & rep == 1, meanonly >> local o1 = r(min) >> local o2 = r(max) >> forvalues i = `z1'/`z2' { >> local isin = 1 >> forvalues o = `o1'/`o2' { >> if inrange(trandate[`i'], wind_start[`o'], >> wind_end[`o']) { >> local isin = 0 >> } >> if `isin' == 1 replace rep_ins = 1 in `i' >> } >> } >> } >> >> Despite countless tries and modifications, I cannot find the mistake >> in the syntax. I simply don't know what is supposed to be wrong here. >> I know this code should be working the way I need it... >> * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/