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: Generating a variable by assigning value form another variable


From   "Vassilopoulos Achilleas" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: Generating a variable by assigning value form another variable
Date   Wed, 13 Jul 2011 09:53:52 +0300

Dear Indu,

If  main_owner  is a string variable just add quotes to its value. Namely,
based on your last example:

id  main_owner  hrs   hrs01   hrs02 hrs03 hrs04 hrs05   hrs06 hrs07 hrs08
hrs09 hrs10
1	  01		.      20       10       .          5         .
.         .         .         .        .
2	  01		.      30        .         .          5         .
.         .         .         .        .
3	  01		.      30        .         .	  .          .
.         .         .         .        .
4	  02		.      25      40      10	  .          .
.         .         .         .        .
5	  01		.      30      20      10	  .          .
.         .         .         .        .


With the following commands:


local xx "01 02 03 04 05 06 07 08 09 10"

foreach x of local xx {
replace hrs =  hrs`x' if main_owner == "`x'"
}


Turns into:

id  main_owner   hrs   hrs01   hrs02 hrs03 hrs04 hrs05   hrs06 hrs07 hrs08
hrs09 hrs10
1	  01	          20      20       10       .          5         .
.         .         .         .        .
2	  01		30      30         .         .         5         .
.         .         .         .        .
3	  01		30      30         .         .	     .          .
.         .         .         .        .
4	  02		40      25       40      10	     .          .
.         .         .         .        .
5	  01		30      30       20      10	     .          .
.         .         .         .        .


which is probably what you want.


Hope this helps,
_____________ - _______________

Achilleas Vassilopoulos

Agricultural University of Athens,
Dept. of Agricultural Economics and Rural Development,
Lab. of Political Economy and European Integration.
Iera Odos 75, 11855, Athens, Greece

Tel: (+30) 210-5294726
Fax: (+30) 2105294786
e-mail : [email protected]  


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Indu Khurana
Sent: Wednesday, July 13, 2011 3:05 AM
To: [email protected]
Subject: Re: st: RE: Generating a variable by assigning value form another
variable

Dear Achilleas,

Thanks for the help!!

But the above code did not work. It still generated all missing values.
I will re-frame and simplify my question: I have divided my data for
each single year.
I don't have any more year suffix( _2, _3....) attached with the variable.

gen hrs=.
 local xx "01 02 03 04 05 06 07 08 09 10"

id main_owner hrs hrs01 hrs02 hrs03 hrs04........hrs10
1	01		.     20   10        .          5
2	01		.     30    .          .         5
3	01		.     30   .	       .	   .       .
4	02		.     25   40     10	   .
5	01		.     30   20      10	   .
.
.
5000 firms..............................................................

(main_owner is a string variable)

Now I want hrs to take value of hrs01 for id 1 ie hrs change from
"missing to 40"
and hrs to take value of hrs002 for  id 4 ie hrs change from "missing to 20"

to look like:

id main_owner hrs hrs01 hrs02 hrs03 hrs04
1	01	      20     20   10        .          5
2	01	      30     30    .          .          5
3	01	      30     30   .	       .	     .       .
4	02	      40     25   40     10	     .
5	01	      30     30   20      10	     ..
.
.
.
I will appreciate your comments.

Thanks!!
Regards

Indu

On Tue, Jul 12, 2011 at 1:42 AM, Vassilopoulos Achilleas
<[email protected]> wrote:
> The problem is within the loop; your notation is a bit sloppy there. This
> should work:
>
> local xx "01 02 03 04 05 06 07 08 09 10"
>
> foreach x of local xx {
>  replace hours_2 =  hours_`x'_2 if main_owner == `x'
> }
>
> Hope this helps,
> _____________ - _______________
>
> Achilleas Vassilopoulos
>
> Agricultural University of Athens,
> Dept. of Agricultural Economics and Rural Development,
> Lab. of Political Economy and European Integration.
> Iera Odos 75, 11855, Athens, Greece
>
> Tel: (+30) 210-5294726
> Fax: (+30) 2105294786
> e-mail : [email protected]
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Indu Khurana
> Sent: Tuesday, July 12, 2011 7:52 AM
> To: [email protected]
> Subject: st: Generating a variable by assigning value form another
variable
>
> Dear Statalisters,
>
> Subject: Generating a variable by assigning value form another variable
>
> I have a panel data of 5000 firms. Each firm can have multiple owners,
> but in the first stage I identified the main owner using the  first
> year data and kept it same for all 6 years.
> This was done through a -reshape- command. ( In the data, if main
> owner==1 means that the first person was the main owner.)
>
> Now I have something like:
> firm_id  main owner   age    gender    hours_1
> 1               1              30       M            20
> 2               2              20        F            30
> 3               1              30        F            35
> 4               1              40        M            20
> 5               1
> Basically this is the subset from year1 of the whole data.
>
> Now I want hours_worked each year to change because it is a  time
> variant variable.
>
> When I merge this data with the rest of the data it looks like:
> firm_id main_owner age gendr hours_1 hours_01_2 hours_02_2
> hours_03_2.. hours_10_2..hours_10_6
> 1               1              30       M            20
> 2               2              20        F            30
> 3               1              30        F            35
> 4               1              40        M            20
> 5               1
>
> hours _01_2 means: hours worked by owner 1 in the second year.
> (hours_owner number* _ year)
> hours_02_2 means: hours worked by owner 2 in the second year.
>
> there are 1 to 10 owners and it goes over for 6 years.
>
> Now i generated 5 more variables hours_2 (for second year), hours_3,
> ....... hours_6
>
> I want hours_2 to take the value from the columns:  hours_01_2
> hours_02_2 hours_03_2  ......... hours_10_2.
>
> If main owner identified in stage 1 is 1 then variable hours_2 should
> take the value from hours_01_2 and if
> If main owner identified in stage 1 is 2 then variable hours_2 should
> take the value from hours_02_2 .....and so on..
>
> I did:
> gen hours_2=.
> gen hours_3 =.
> ......
>
> local xx "01 02 03 04 05 06 07 08 09 10"
>
> for each x of local xx {
>  replace hours_2 =  hours_`x' if main_owner = " `x'"
> }
>
>
> But this does not work.
>
> Please guide.
>
> Thanks!!
> *
> *   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/
>
> *
> *   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/
>

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


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