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: Question about fillin


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Question about fillin
Date   Thu, 14 Feb 2013 23:24:37 +0000

This is a better method than my previous. It does use -fillin-. Unlike
my previous solution, it does not assume implicitly that there are no
spaces in the stock names.

* sandpit to play in
clear
input individual str1 stock period
1             C        1
1             E        1
1             A        2
1             F        2
2             G        1
2             H        1
2             I        2
2             K        2
end

* This does the work
egen group = group(individual stock)
fillin period group
bysort group (_fillin) : replace indiv = indiv[_n-1] if mi(indiv)
bysort group (_fillin) : replace stock = stock[_n-1] if mi(stock)

* How did we do?
list individual stock period, sepby(indiv)

The key to using -fillin- is thus to bind -individual- and -stock- in
one variable. Some clean-up afterwards is needed.

Nick

On Thu, Feb 14, 2013 at 3:39 PM, Nick Cox <[email protected]> wrote:
> -fillin- doesn't support -if- or -in- either, so a loop over groups is
> out of the question (without some awful choreography of files).
>
> This is an in-place solution. The use of -contract- as an expansion
> command is perhaps counter-intuitive, but it works for your example.
> History bites me here, as the name -contract- was my suggestion.
>
> If you have other variables you are not telling us about, as seems all
> too likely, I guess that makes things no more difficult than the need
> for a -merge- as well.
>
> . egen group = concat(ind stock), p(" ")
>
> . contract period group, zero
>
> . split group, destring
> variables born as string:
> group1  group2
> group1 has all characters numeric; replaced as byte
> group2 contains nonnumeric characters; no replace
>
> . rename (group?) (individual stock)
>
> . gen _fillin = 1 - _freq
>
> . drop _freq group
>
> . sort individual stock period
>
> . list individual stock period, sep(8)
>
>      +---------------------------+
>      | indivi~l   stock   period |
>      |---------------------------|
>   1. |        1       A        1 |
>   2. |        1       A        2 |
>   3. |        1       C        1 |
>   4. |        1       C        2 |
>   5. |        1       E        1 |
>   6. |        1       E        2 |
>   7. |        1       F        1 |
>   8. |        1       F        2 |
>      |---------------------------|
>   9. |        2       G        1 |
>  10. |        2       G        2 |
>  11. |        2       H        1 |
>  12. |        2       H        2 |
>  13. |        2       I        1 |
>  14. |        2       I        2 |
>  15. |        2       K        1 |
>  16. |        2       K        2 |
>      +---------------------------+
>
> On Thu, Feb 14, 2013 at 2:16 PM, André Gyllenram
> <[email protected]> wrote:
>
>> I have a question about the command fillin.
>>
>> It is not possible to combine this with by. But can you do in some other way?
>>
>> I would like to write like this:
>>
>> by individual: fillin individual stock period.
>>
>>
>> My data looks like this;
>>
>> INDIVIDUAL    STOCK   PERIOD
>> 1             C        1
>> 1             E        1
>> 1             A        2
>> 1             F        2
>>
>> 2             G        1
>> 2             H        1
>> 2             I        2
>> 2             K        2
>>
>> And I would like to add observations with missing data so that in every time period there is a row for all stocks that the individual owns in any period. Individual 1 owns
>> stock C, E, A and F in period 1 or 2. This means that i want a row for these stocks in every time period for individual 1.
>>
>> Individual 2 owns stock G, H, I an K in period 1 or 2. So for every time period I want a row for these stocks for individual 2. In this example I want to add observations so the data looks like this:
>>
>>
>> INDIVIDUAL   STOCK   PERIOD
>> 1            A       1
>> 1            C       1
>> 1            E       1
>> 1            F       1
>> 1            A       2
>> 1            C       2
>> 1            E       2
>> 1            F       2
>>
>> 2            G       1
>> 2            H       1
>> 2            I       1
>> 2            K       1
>> 2            G       2
>> 2            H       2
>> 2            I       2
>> 2            K       2
>>
>> So in this example I want to add 8 rows. If I write fillin individual period I would add much more rows than i want to since the individuals together own stocks A, C, E, F, G, H, I
>> and K.

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