Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Using one variable to label another


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Using one variable to label another
Date   Mon, 20 Nov 2006 13:17:08 -0000

The principle is right, but you'll need to tweak 
what is here, which won't work. 

Each line 

 	label define mylab v1[`n'] "v2[`n']", modify

should be more like 

 	label define mylab `=v1[`n']' "`=v2[`n']'", modify

Note that here there are no checks here on what should be
the case. 

The -labmask- program mentioned earlier has more checks. 
But don't cut and paste from here. Install -labutil- from SSC. 

------------------------------------ labmask.ado 
*! NJC 1.0.0 20 August 2002
* values of -values-, or its value labels, to be labels of -varname-
program def labmask, sortpreserve 
	version 7 
	syntax varname(numeric) [if] [in], /* 
	*/ VALues(varname) [ LBLname(str) decode ]

	* observations to use 
	marksample touse 
	qui count if `touse' 
	if r(N) == 0 { 
		error 2000 
	}	
	
	* integers only! 
	capture assert `varlist' == int(`varlist') if `touse' 
	if _rc { 
		di as err "may not label non-integers" 
		exit 198 
	}
	
	tempvar diff decoded group example 
	
	* do putative labels differ? 
	bysort `touse' `varlist' (`values'): /* 
		*/ gen byte `diff' = (`values'[1] != `values'[_N]) * `touse' 
	su `diff', meanonly 
	if r(max) == 1 { 
		di as err "`values' not constant within groups of `varlist'" 
		exit 198 
	} 

	* decode? i.e. use value labels (will exit if value labels not assigned) 
	if "`decode'" != "" { 
		decode `values', gen(`decoded') 
		local values "`decoded'" 
	} 	

	* we're in business 
	if "`lblname'" == "" { 
		local lblname "`varlist'" 
	} 
	
	* groups of values of -varlist-; assign labels 
	
	by `touse' `varlist' : /*
		*/ gen byte `group' = (_n == 1) & `touse' 
	qui replace `group' = sum(`group') 

	gen long `example' = _n 
	local max = `group'[_N]  
	
	forval i = 1 / `max' { 
		su `example' if `group' == `i', meanonly 
		local label = `values'[`r(min)'] 
		local value = `varlist'[`r(min)'] 
		label def `lblname' `value' `"`label'"', modify 	
	} 

	label val `varlist' `lblname' 
end 
--------------------------------- 

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

Jeph Herrin
 
> Not efficient, but effective:
> 
>   local N=_N
>   forv n=1/`N' {
> 	label define mylab v1[`n'] v2[`n'], modify
>   }
>   label values v1 mylab
> 
> If your dataset is large, you may want to first
> 
>   preserve
>   bys v1 : keep if _n==1
>   local N=_N
>   forv n=1/`N' {
> 	label define mylab v1[`n'] "v2[`n']", modify
>   }
>   restore
>   label values v1 mylab
> 
> 
Doogar, Rajib wrote:
> > In Stat 9.2, how can I use the values of v2 (str 40, say) 
> to label v1
> > (int)?  My data looks something like this:
> > 
> > V1      V2
> > 1234    somerandomstring1
> > 1234    somerandomstring1
> > 2345    somerandomstring2
> > ....
> > 6578    somerandomstringn
> > ....
> > etc.
> > 
> > I tried to encode v2 and then attach the resulting label to v1.  I
> > encoded v2 all right, but then got stumped by how correctly 
> to link the
> > labels to the values of v1.  Encoding v2 gave me labels 
> numbered 1 to
> > xxx, but v1 is a four digit number (although it does take 
> exactly xxx
> > distinct values).

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