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
Indu Khurana <[email protected]>
To
[email protected]
Subject
Re: st: RE: Creating a new variable via another variable name based on a condition
Date
Thu, 15 Jul 2010 00:41:55 -0400
Hello,
Subject: How to reshape the data from wide to long form.
I am still stuck up at reshaping the data set.
There are 2 variables Hours worked by the owner and equity percentage:
Both have 2 parts :
a) Number of owners are changing
b) Years are also changing
Problem: Creating a common stub.
This is how they appear in the dataset.
total_hours_owner_01_r_0 and f6_perc_owned_owner_01_0
total_hours_owner_02_r_0 and f6_perc_owned_owner_02_0
.
.
.
total_hours_owner_14_r_4 and f6_perc_owned_owner_14_4
(01 and 02 represent owner number and 0 at the end show the year no.)
This is how I did to create a common stub
I renamed the variables as
tot_hours_owner001 tot_hours_owner002..... tot_hours_owner301....
tot_hours_owner414
001 means 1st year and 1st owner
002 means 1st year and 2nd owner and so on
414 means 4th year and 14th owner....
Similarly with equity variable
f6_perc_owned_owner001 f6_perc_owned_owner002
......f6_perc_owned_owner310.....f6_perc_owned_owner414
when i give command
**** clear ***
reshape tot_hours_owner f6_perc_owned_owner , i(firmid) j(year) string
***********or**************
reshape tot_hours_owner@ f6_perc_owned_owner @, i(firmid) j(year) string
***********
Both of them does not give me the answer. There seems to be a problem
with one on one identification of suffixes of variable.
Please guide!!
Thanks
On Sat, Jul 10, 2010 at 3:40 AM, Indu Khurana <[email protected]> wrote:
> Thanks Nick and Eric. I am trying to figure out how to do this.
>
> On Fri, Jul 9, 2010 at 12:06 PM, Nick Cox <[email protected]> wrote:
>> A problem that remains is that Indu specified no rules for cases in
>> which both time and equity are tied among two or more people.
>>
>> Such firms could be identified after my code by
>>
>> by id : gen ties = hours[_N] == hours[_N-1] & equity[_N] == equity[_N-1]
>>
>> l if ties
>>
>> Nick
>> [email protected]
>>
>> Eric Booth
>>
>> Nevermind on the last part about Nick's code -- I missed his line
>> "mvencode *, mv(0) override".
>>
>>
>> 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=.)
>>> ********
>>
>> Nick Cox
>>
>>>> (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.
>>
>> 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/
>>
>
>
>
> --
> Indu Khurana
>
> Department of Economics, DM-308A
> Florida International University
> Miami, FL 33199
> Web: http://www.fiu.edu/~ikhurana
>
--
Indu Khurana
Department of Economics, DM-308A
Florida International University
Miami, FL 33199
Web: http://www.fiu.edu/~ikhurana
*
* 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/