Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[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" <vassilopoulos.statalist@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
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 : avassilopoulos.aua@gmail.com    



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index