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: RE: Creating a new variable via another variable name based on a condition


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: RE: Creating a new variable via another variable name based on a condition
Date   Thu, 8 Jul 2010 20:24:54 +0000

<>

Nevermind on the last part about Nick's code -- I missed his line "mvencode *, mv(0) override".  

~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754
Fax: +979.845.0249
http://ppri.tamu.edu

On Jul 8, 2010, at 3:17 PM, Eric Booth wrote:

> <>
> 
> I also reshaped to long but approached it a different (and longer way) than Nick.
> 
> ********!
> clear
> inp firms hrs_owner1 hrs_owner2 hrs_owner3 equity_owner1 equity_owner2 equity_owner3 
> 1 3  2  0 50  25 25 
> 2 2  3  6 25  50 25 
> 3 3  8  . 75  25 . 
> 4 2  .  2 80  . 20 
> end
> **reshape**
> reshape long hrs_owner equity_owner, i(firms) j(ownerid)
> **find max**
> 	bys firms: egen maxhours = max(hrs_owner)
> **find ties**
> 	g main_owner = ownerid if maxhours==hrs_owner
> 	bys firms: egen maxequity = max(equity_owner) if !mi(main_owner)
> **replace main_owner with tied owner with most equity**
> 	bys firms: replace main_owner = . if maxequity != equity_owner
> 	bys firms (main_owner): replace main_owner = main_owner[1]
> 	drop max*
> ********!
> 
> but I got a different answer than when I use Nick's solution (translated into my varnames):
> 
> bysort firms (hrs_ equity_) : gen main = ownerid[_N] 
> 
> If I've translated the varnames correctly, I think Nick's command can assign main ownership to ids with missing data.  There might be a better way to fix Nick's solution, but one way looks to be to recode the missings to zero:
> 
> ********
> recode hrs_ equity_ (.=0)
> bysort firms (hrs_ equity_) : gen main = ownerid[_N] 
> recode hrs_ equity_ (0=.)
> ********
> 	
> ~ Eric
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> [email protected]
> 
> On Jul 8, 2010, at 2:46 PM, Nick Cox wrote:
> 
>> (You should -update- to 8.2. It's free.) 
>> 
>> You should -reshape- your data to -long-. Then no loops are required. 
>> 
>> You don't give variable names. I will assume 
>> 
>> Firm identifier: id 
>> Hours worked by owners 1, 2, etc.: hours1, etc. 
>> Equity owned by owners 1, 2, etc.: equity1, etc. 
>> 
>> The -reshape- is 
>> 
>> reshape long hours equity, i(id) 
>> 
>> Then 
>> 
>> rename _j owner
>> mvencode *, mv(0) override
>> bysort id (hours equity) : gen main = owner[_N]
>> 
>> Repeat: no loops. 
>> 
>> Nick 
>> [email protected] 
>> 
>> Indu Khurana
>> 
>> Creating a new variable via another variable name based on a condition
>> 
>> Using stata 8.0. I am trying to create a logical condition which would
>> copy the name of the variable if satisfied.
>> 
>> Here is what I have:
>> 
>> Aim is to identify the main owner. I have generated one variable Main
>> Owner
>> I assign value to this variable using maximum no. of hours worked.
>> 
>> I have information on
>> 1. Total 10 owners. Sometimes there may be only 1 or 2 owners.
>> 2. Hours - Number of hours worked per week.
>> 3. Equity - Equity ownership by each worker.
>> I have data for 5 years. There are approximately 5000 firms.
>> 
>> There are 2 issues:
>> 1. Lets say there are 2 owners (1 and 2) . Owner1 works for 3 hours
>> and Owner2 works for 2 hours, then I want the variable main owner to
>> take the value 1. How can I post the name of the variable and not the
>> value?
>> 2. Now if the hours worked for the owners are the same I use Equity
>> ownership as the variable to resolve the tie. In that case owner with
>> maximum equity ownership becomes the main owner. How to create a loop
>> for this?
>> 
>> 1st year data
>> 
>> 
>> Firms Hrs owner1 Hrs owner2  Hrs owner3  main owner
>> 1             3                2               0                  1
>> 2              2               3              6                   3
>> 3              3               8               .                   2
>> 4              2                .              2         tie - go to
>> equity
>> .
>> .
>> .
>> 5000
>> 
>> Firms Equity ownr1  Equity ownr2  Equity owner3  main ownr
>> 1                50                25                      25         1
>> 2                25                50                      25         3
>> 3                75                25                       .          2
>> 4                80                 .                       20         1
>> .
>> .
>> .
>> 5000
>> 
>> 
>> *


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