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]
st: RE: Generating a variable by assigning value form another variable
From 
 
"Vassilopoulos Achilleas" <[email protected]> 
To 
 
<[email protected]> 
Subject 
 
st: RE: Generating a variable by assigning value form another variable 
Date 
 
Wed, 13 Jul 2011 10:17:22 +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]    
*
*   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/