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: Replacing values


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Replacing values
Date   Wed, 10 Jul 2013 14:54:54 +0100

That's correct. I didn't notice the repeated values of -state
district-; sorry about that.
Nick
[email protected]


On 9 July 2013 23:43, Roberto Ferrer <[email protected]> wrote:
> Thanks Nick.
>
> The second solution worked.
>
> The first does not because it adds up the values of pop_region by
> state and district. Substituting total() for min(), max(), mean(),
> etc. works since the values involved are constants:
>
> egen pop2005 = min(pop_region / (year == 2005)), by(state district)
>
> (Trying to get my hands on the reference you gave.)
>
> Thanks again.
>
> On Tue, Jul 9, 2013 at 10:32 PM, Nick Cox <[email protected]> wrote:
>> egen pop2005 = total(pop_region / (year == 2005)), by(state district)
>> replace pop_region = pop2005 if missing(pop_region)
>>
>> In your example, this works
>>
>> bysort state district (year) : replace pop_region = pop_region[_N]
>>
>> For a review of technique see
>> http://www.stata-journal.com/article.html?article=dm0055
>> Nick
>> [email protected]
>>
>>
>> On 9 July 2013 22:12, Roberto Ferrer <[email protected]> wrote:
>>> For every possible year in the data, I want to replace the missing
>>> values of the variable pop_region using the corresponding value of
>>> variables state and district of year 2005. So the first value of
>>> pop_region in the example
>>> should be 25, the second 25, ..., the sixth 50, the eighth 62, etc.
>>>
>>> year  state  district  st_pop  pop_region
>>> 1995   a         1           100         .
>>> 1995   a         2           100         .
>>> 1995   a         2           100         .
>>> 1995   a         2           100         .
>>> 1995   a         2           100         .
>>> 1995   a         3           100         .
>>> 1995   a         3           100         .
>>> 1995   b         1           200         .
>>> --------------------------------------------------------
>>> 2005   a         1           100         25
>>> 2005   a         2           100         25
>>> 2005   a         3           100         50
>>> 2005   a         3           100         50
>>> 2005   b         1           200         62
>>> 2005   b         1           200         62
>>> 2005   b         1           200         62
>>> 2005   b         1           200         62
>>>
>>> I tried this (and other stuff) which seems too involved for the task
>>> and doesn't work of course:
>>>
>>> levelsof year, local(lyear)
>>> levelsof state, local(lstate)
>>> levelsof district, local(ldistrict)
>>>
>>> foreach y of local lyear {
>>>     foreach s of local lstate {
>>>         foreach r of local ldistrict {
>>>              local reval = pop_region if yearobs==2005 & state==`s' &
>>> district==`r'
>>>              replace pop_region = `reval' if state == `s' & district == `r'
>>>         }
>>>     }
>>> }
>>>
>>> Thanks.
>>> *
>>> *   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/
>> *
>> *   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/
> *
> *   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/
*
*   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