Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: Re: Re: st: creating new variable |

Date |
Thu, 13 Oct 2011 13:48:16 +0100 |

The code worked for me. You must be careful not to split lines. count if gvkey == gvkey[`i'] & inrange(appyear, appyear[`i'] -5, appyear[`i'] -1 ) is all one command. If that is not the explanation, then I can only suggest that you copied the code incorrectly. On Thu, Oct 13, 2011 at 1:43 PM, S.H. Former <S.H.Former@uvt.nl> wrote: > Again thanks for your help! Stata gives the code: too few ')' or ']', r(132). I have looked at the command but couldn't really find a fault in it but I must say that I am not the biggest expert in it ;) Maybe you know why it is giving me this error? > > kind regards, > > Sytze > > > -----Original Message----- > From: Nick Cox <njcoxstata@gmail.com> > To: statalist@hsphsun2.harvard.edu > Date: Thu, 13 Oct 2011 12:26:44 +0100 > Subject: Re: Re: st: creating new variable > > Try > > gen count = 0 > > qui forval i = 1/`=_N' { > count if gvkey == gvkey[`i'] & inrange(appyear, appyear[`i'] -5, > appyear[`i'] -1 ) > replace count = r(N) in `i' > } > > This is clumsy and will execute slowly, but the logic should be easy to follow. > > Nick > > On Thu, Oct 13, 2011 at 12:13 PM, Nick Cox <njcoxstata@gmail.com> wrote: >> You are quite right; I overlooked the more difficult bit which was >> clearly stated. >> >> Some technique is discussed at >> >> SJ-7-3 pr0033 . . . . . . . . . . . . . . Stata tip 51: Events in intervals >> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox >> Q3/07 SJ 7(3):440--443 (no commands) >> tip for counting or summarizing irregularly spaced >> events in intervals >> >> That's accessible to you via the Stata Journal website. >> >> On Thu, Oct 13, 2011 at 11:58 AM, S.H. Former <S.H.Former@uvt.nl> wrote: >> >>> Thanks ver much for your reaction, >>> >>> The only thing what remains unknown for me is how to count only the observations from the last five years for each gvkey. I have looked at the tutorial you included but still I couldn't really find out! >>> >>> best regards, >>> >>> Sytze >>> >>> -----Original Message----- >>> From: Nick Cox <njcoxstata@gmail.com> >>> To: statalist@hsphsun2.harvard.edu >>> Date: Wed, 12 Oct 2011 18:10:42 +0100 >>> Subject: Re: st: creating new variable >>> >>> Yes, it's possible. Here's one way >>> >>> bysort gvkey (appyear) : gen newvar = _n-1 if appyear != appyear[_n-1] >>> bysort gvkey : replace newvar = newvar[_n-1] if missing(newvar) >>> >>> See >>> >>> SJ-2-1 pr0004 . . . . . . . . . . Speaking Stata: How to move step by: step >>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox >>> Q1/02 SJ 2(1):86--102 (no commands) >>> explains the use of the by varlist : construct to tackle >>> a variety of problems with group structure, ranging from >>> simple calculations for each of several groups to more >>> advanced manipulations that use the built-in _n and _N >>> >>> and >>> >>> FAQ . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values >>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox >>> 9/05 How can I replace missing values with previous or >>> following nonmissing values or within sequences? >>> http://www.stata.com/support/faqs/data/missing.html >>> >>> for the ideas behind the first and second commands. >>> >>> Nick >>> >>> * * 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/

**References**:**Re: Re: Re: st: creating new variable***From:*"S.H. Former" <S.H.Former@uvt.nl>

- Prev by Date:
**RE: st: Interpretation of nldecompose regression output** - Next by Date:
**Re: Re: Re: st: creating new variable** - Previous by thread:
**Re: Re: Re: st: creating new variable** - Next by thread:
**st: control variables** - Index(es):