Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: series of value labels and collapse


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: series of value labels and collapse
Date   Mon, 7 Jul 2003 15:36:57 +0100

akonseig@uni-bonn.de

> I am having trouble with a programme I wrote for specific
> data management
> purpose. I have 271 farmers who may combine at most 3 types
> of crop c1 c2 c3.
> The data is in long format and therefore ci records in
> column the type of
> crop per plot owned by the farmer and in row ci show the
> mixture of crop by
> the farmer in each of the plot. A lot of missing values
> appear of course in
> the data.
> My first objective was therefore to create dummies ci_di
> per crop (maximum of
> 46 types of crop) and this is done through the first
> section of my do file
> below but in my attempt to pass a label value that identify
> each type of crop
> I got the error : label beans already defined
> r(110);
> I actually used some resources of Statalist (posted by Nick
> but on different
> issues) to automate the labelling.

> *--------STEP1: tab, generating dummies per crop and
> labelling their values-
> version 7.0
> use ctypes, clear
> program define compo
> foreach var of local 0{
> sort id
> tab `var', gen(`var'_d)
>
> *below the local cropnames is a sample of the 46 types of
> crops I have. and
> *at the end of the program I pasted the error generated
>
> local cropnames "beans	cassava maize rice watermelon
> macaxeira  maxixe melon
> raizman tomato jambu goma cucumber Cheiroverde sweetpotato
> tabaco Jerimum
> PimentaCheiro Pimenta Doce Beringela"
>
> local codes "beans cassava maize rice watermelon macaxeira
> maxixe melon
> raizman tomato jambu goma cucumber Cheiroverde sweetpotato
> tabaco Jerimum
> PimentaCheiro Pimenta Doce Beringela"
>
> local i = 1
> foreach x of local cropnames {
>         local y : word `i' of `codes'
>         lab define `x' 1 "`y'" 0 "no"
> 	  capture forvalues j=1/46{
> 	  label val `var'_d`j' `x'
> 	  sum  area if  `var'_d`j'==1
> 	  }
> 	  local i = `i' + 1
> }
> end
> local area "c1 c2  c3"
> foreach k of local area {
>  compo `k'
>  }
> program drop compo
> exit
> label beans already defined
> r(110);

I think this is solved by

lab def `x' 1 "`y'" 0 "no", modify

> The next section of the do file simply collapse by crop
> type to get the mean
> value of area croped. Here also I got more than hundred
> data files. Do I have
> to go through each file to display the output of interest
> (mean value of area
> croped) or is there any way I can gather these results all together?
> During the survey the farmer could not know the portion of
> land allocated to
> each crop and one methodological question is to know if it
> is a good idea to
> simply divide the total land by the number of crops
> cultivated on the plot.
> I would appreciate any help from the list.

> *------------------STEP 2: mean value of the area croped by
> crop type.
> *Everything works fine but how to put together the outputs
> generated by
> *collapse--------------------------
>
> version 7.0
> program define compo2
> foreach var of local 0 {
> 	use ctypes, clear
> 	sort id
> 	foreach v of var * {
> 	local l`v' : variable label `v'
>       if `"`l`v''"' == "" {
> 	local l`v' "`v'"
>  	}
>       }
> 	preserve
> 	capture forvalues i = 1/46  {
>       use ctypes, clear
>       collapse area if  `var'_d`i'==1
> 	foreach v of var * {
> 	label var `v' "mean value of `l`v'' `var'_d`i'"
> 	}
> 	keep  id plot area fareacrop
> 	save f`var'`i', replace
> 	}
> 	restore
> }
> end
> local area "c1 c2 c3"
> foreach j of local area {
>  compo2 `j'
>  }
> program drop compo2
> exit

I suspect the easiest way to get the mean
value is through -summarize-: you don't
need to -collapse-.

Nick
n.j.cox@durham.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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   |   What's new   |   Site index