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

st: RE: obtaining entire contents of a value label


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: obtaining entire contents of a value label
Date   Wed, 1 Dec 2004 18:29:28 -0000

I can't think of a method different from 
yours. However, about half the code of -labvalclone- in 
-labutil- appears relevant. Here is all the code. 

As you guess, I guess you need to save the whole label to a file 
and then process the contents, via -file- or otherwise. 

*! NJC 1.1.1 3 Nov 2002 
* NJC 1.1.0 1 Nov 2002 
program def labvalclone
	version 7 
	args old new garbage 
	if "`old'" == "" | "`new'" == "" | "`garbage'" != "" { 
		di as err "syntax is: " /* 
		*/ as txt "labvalclone {it:vallblname newvallblname}" 
		exit 198 
	} 
	if "`old'" == "label" | "`old'" == "define" { 
		di as err "won't work if {txt:`old'} is existing value label name" 
		exit 198 
	}	
	capture label list `new' 
	if _rc == 0 { 
		di as err "value labels {txt:`new'} already exist" 
		exit 198
	} 
	
	tempfile file1 file2
	tempname in out 

	qui label save `old' using `"`file1'"' 
	file open `in' using `"`file1'"', r
	file open `out' using `"`file2'"', w
	file read `in' line
	
	while r(eof) == 0 {
		local line: subinstr local line "`old'" "`new'"
		file write `out' `"`line'"' _n
		file read `in' line
	}
	file close `out'
	
	qui do `"`file2'"'   
end 

Nick 
[email protected] 

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of Phil Schumm
> Sent: 01 December 2004 18:21
> To: [email protected]
> Subject: st: obtaining entire contents of a value label
> 
> 
> Does anyone know if it's possible to obtain the entire contents of a 
> value label, say with something like a macro extended function?  What 
> I'd like to do is to step through the entire mapping 
> programmatically, and thus actually, even just obtaining the list of 
> integers mapped to labels (i.e., a sort of -levels- command for value 
> labels) would also work, since I could then use the label macro 
> extended function to obtain the label for each integer.
> 
> I've looked at the other commands that operate on value labels, but 
> these (e.g., -labelbook- or -numlabel-) appear to use -uselabel-, 
> which itself uses -label save- to create a file containing the 
> mapping(s) and then parses this file.  I can do this too, but in 
> cases where I just want a single mapping, it seems like a lot of work 
> to create a file and then read it.
> 
> If I am overlooking something obvious here, I apologize.

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index