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: Copying data from an observation to a different time period, using bysort


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Copying data from an observation to a different time period, using bysort
Date   Fri, 1 Nov 2013 00:09:22 +0000

Replacing the previous value in general results in a cascade of
replacements. That is discussed in detail at

<http://www.stata.com/support/faqs/data-management/replacing-missing-values/>

When you want just one replacement, you need something different.

Try this. The first technique is discussed within my paper "Compared
with ..." in Stata Journal 11(2). The reversing time trick is
explained in the FAQ just cited.

bysort co_id yrmonth: egen tocopy = total(quintile / (rtype == "formation"))

gen negyrmonth = -yrmonth

bysort co_id formation (negyrmonth) : replace quintile = tocopy[_n+1]
if missing(quintile) & rtype == "buy_sell_period"



Nick
[email protected]


On 31 October 2013 19:25, Clarice Martins <[email protected]> wrote:

> I have been trying to solve a new issue in my dataset:  (I am using Stata 12, for Mac)
>
> I am using command -bysort  varlist : command-
> (but not sure if it is the best option)
>
> The problem is to copy -quintile- observation to NEXT -yrmonth- , - if rtype=="buy_sell_period"-
>
> *-------snapshot of dataset-----
> co_id   ticker  name    rtype   yrmonth rating  numericrating   return  quintile
> 2       AEDU3   ANHANGUERA      buy_sell_period 2007m8  BB      12      -0.02 .
> 2       AEDU3   ANHANGUERA      formation       2007m8  BB      12      0.32 1
> 2       AEDU3   ANHANGUERA      hold_period     2007m8  BB      12      0.32 .
> 2       AEDU3   ANHANGUERA      buy_sell_period 2007m9  BB      12      0.13 .
> 2       AEDU3   ANHANGUERA      formation       2007m9  BB      12      0.40 5
> 2       AEDU3   ANHANGUERA      hold_period     2007m9  BB      12      0.40 .
> *-------------
>
> Expected result:
>
> - In this case, the quintile 1 needed to go to 2007m9, buy_sell_period.
> - And quintile 5 need to go to 2007m10, buy_sell_period (not shown)
>
> I tried a mix of several things in these lines, but not quite getting the results needed:
>
>     bysort co_id yrmonth (quintile) : generate quintile2 = quintile[_n-1] // the result is quintile copied in sequence not what is needed
>
> I wonder if I need to change HRF dates to SIF dates? (Since sorting by -yrmonth- does produce a calendar order of months and year.)
*
*   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