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: Re: Re: st: creating new variable


From   "S.H. Former" <[email protected]>
To   [email protected]
Subject   Re: Re: Re: st: creating new variable
Date   Thu, 13 Oct 2011 14:43:41 +0200

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 <[email protected]>
To: [email protected]
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 <[email protected]> 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 <[email protected]> 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 <[email protected]>
>> To: [email protected]
>> 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
>>
>>
>> On Wed, Oct 12, 2011 at 5:17 PM, S.H. Former <[email protected]> wrote:
>>
>>> I have a question regarding the composition of my database. I want to know whether it is possible to incorporate the following variable. I want to create a variable that counts previous observations but only till five years back. Let me explain it using a part my database which looks as follows:
>>>
>>> gvkey   appyear
>>> 1004    1985
>>> 1004    2001
>>> 1010    1977
>>> 1010    1977
>>> 1010    1977
>>> 1010    1977
>>> 1010    1977
>>> 1010    1977
>>> 1010    1977
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1978
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>> 1010    1979
>>>
>>> Now I want a new variable that counts the number of observations for each Gvkey (so it has to be counted for each Gvkey) where the number of observations is counted until five years back. So the new variable should look like this:
>>>
>>>  gvkey  appyear                 New Variable            Comment
>>> 1004    1985                    0                        no observations before
>>> 1004    2001                    0                       longer than five years ago
>>> 1010    1977                    0                       no observations
>>> 1010    1977                    0                       no observations
>>> 1010    1977                    0                       no observations
>>> 1010    1977                    0                       no observations
>>> 1010    1977                    0                       no observations
>>> 1010    1977                    0                       no observations
>>> 1010    1977                    0                       no observations
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1978                    7                       number of observations in 1977 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> 1010    1979                    18              number of observation in 1977 and 1978 by Gvkey 1010
>>> Of course, the comment would not have to be included but is to make the problem clear to you! Hope you can help me,
>>
>

*
*   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/


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