Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: RE: Looping within a subset under a certain condition


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: RE: Looping within a subset under a certain condition
Date   Sat, 29 Sep 2012 02:38:24 +0100

Stata, not STATA.

I don't understand what you are trying to do but

local r = obs[`i'] if rep[`i'] == 1
local z = `i'+1 if rep[`i'] == 1

makes no sense. What you mean is perhaps

if rep[`i'] == 1 {
          local r = obs[`i']
          local z = `i'+1
}

On Sat, Sep 29, 2012 at 12:31 AM, Gerard Solbrig
<[email protected]> wrote:
> This is what I have come up with so far, but for the second -forvalues-
> loop, STATA tells me that "invalid syntax r(198)"!
> I guess the problem lies with the local macro z, but I'm not sure.
> Any troubleshooting idea? I'm not even sure if that get's me where I want to
> go...
>
> gen rep_ins = .
> gsort +firm_id -rep
> by firm_id: gen obs = _n
> gen group_obs = _n
> sort firm_id
> by firm_id (group_obs): replace group_obs = group_obs[1]
> by group_obs, sort: gen group = _n == 1
> replace group = sum(group)
> summarize group, meanonly
> local max = r(max)
> forvalues x = 1/`max' {
>         local N`x' = obs[_N]
> }
> /*how to make the counting macro for the forvalues look dynamic by firm_id
> group? */
> /* works fine up to here! */
>
> forvalues i = 1/`N`x'' {
>         local r = obs[`i'] if rep[`i'] == 1
>         local z = `i'+1 if rep[`i'] == 1
>                 if trandate[`z'] >= wind_start[`r'] & trandate[`z'] <=
> wind_end[`r'] {
>                 replace rep_ins = 1 in [`z']
>                 }
>         else {
>         replace rep_ins = 0 in [`z']
>         }
> }
>
> Thanks in advance.
> Best,
> Gerard Solbrig
>
>
> -----Original Message-----
> From: Gerard Solbrig [mailto:[email protected]]
> Sent: Freitag, 28. September 2012 22:45
> To: '[email protected]'
> Subject: Looping within a subset under a certain condition
>
> Dear Statalist-Members,
>
> I'm encountering a problem for which I seek your help.
>
> Let me start off with an example from my data (what I want it to look like
> in the end), before I explain my particular problem.
>
> firm_id date            rep     wind_start              wind_end
> rep_ins
>
> firm1           01jan2000       0       .                       .
> 0
> firm1           05apr2004       0       .                       .
> 1
> firm1           01nov2004       1       05may2004               30may2005
> .
> firm1           10dec2004       0       .                       .
> 1
> firm1           01jan2006       0       .                       .
> 0
> firm2           30dec1999       1       03jul1999               27jul2000
> .
> firm2           05jan2000       1       09jul1999               02aug2000
> .
> firm2           06jun2000       0       .                       .
> 1
>
> Each firm in my data has a 'firm_id'. Variable 'date' refers to an event
> date. The 'rep' dummy indicates the type of event.
> I set 'wind_start' and 'wind_end' as period around the event
> (-180days,+210days), in case it's a rep = 1 type event.
>
> Now, I would like the 'rep_ins' dummy to indicate (i.e., rep_ins = 1),
> whether the date of all other observations of this firm (where rep = 0) lies
> within the range determined by 'wind_start' and 'wind_end' (which is
> conditional upon the 'rep' dummy).
>
> I've come across looping over observations and tried to design a solution
> for this problem based on that, but failed to do so. I assume the solution
> also depends on sorting the data in a special way.
>
> Here's the first part of my .do-file:
>
> gen wind_start = date-180 if rep == 1
> gen wind_end = date+210 if rep == 1
> format wind_start %d
> format wind_end %d
> gsort +firm_id +date +trandate
> gen rep_ins = 0 if rep != 1
>
> I tried to come up with a solution by adding variables 'per_start' and
> 'per_end' for all rep = 0:
>
> gen per_start = date-180 if rep == 0
> gen per_end = date+180 if rep == 0
> format per_start %d
> format per_end %d
>
> To mark the period within which the rep = 1 event can lie. Maybe this could
> contribute to finding a solution as well.
>
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index