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]

Re: st: Combining value labels, appending


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Combining value labels, appending
Date   Thu, 12 May 2011 10:18:57 +0100

Here's a program, -labvalcombine-. Not much testing!

Examples first:

. label def lb1 1 "one" 2 "two" 3 "three", modify
. label def lb2 2 "deux" 3 "three" 4 "four", modify

. labvalcombine lb1 lb2, lblname(both)
both:
           1 one
           2 deux
           3 three
           4 four

. labvalcombine lb1 lb2
lb1:
           1 one
           2 deux
           3 three
           4 four

The rules are

1. Specify two or more names of value label sets.

2. If a -lblname()- is specified, that is used for the combined
labels. Otherwise the first label set name is used.

3. Later definitions overwrite earlier definitions.

If you don't like the rules, feel free to devise alternatives.

Nick

*! NJC 1.0.0 12 May 2011
program labvalcombine
	version 8
	gettoken names 0 : 0 , parse(,)
	syntax [, lblname(str)]

	foreach name of local names {
		capture label list `name'
		if _rc {
			di as err "value label `name' not found"
			exit _rc
		}
	}	

	tempfile dofile1 dofile2
	tempname in out
	qui label save `names' using `"`dofile1'"'
	file open `in' using `"`dofile1'"', r
	file open `out' using `"`dofile2'"', w

	file read `in' line
	tokenize `"`line'"'
	if "`lblname'" == "" local lblname `3'

	local cmd "label define `lblname'"
	
	while r(eof) == 0 {
		local line : subinstr local line "`1' `2' `3'" "`cmd'"
		file write `out' `"`line'"' _n
		file read `in' line
		tokenize `"`line'"'
	}
	file close `out'
	
	qui do `"`dofile2'"'
	label list `lblname'
end


On Thu, May 12, 2011 at 8:56 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> These are, strictly, sets of value labels.
>
> Something could be written to do this. A good program would indeed
> check for overlap and have rules to determine what overwrites what.
>
> See also -multencode- from SSC.
>
> A quick and dirty way for the easy case:
>
> forval i = 16/30 {
> local label : label numbers2 `i'
> label define numbers1 `i' `"`label'"' , add
> }
>
> Nick
>
> 2011/5/12 Jorge Eduardo Pérez Pérez <perez.jorge@ur.edu.co>:
>>
>> Is there a way to combine two value labels into a single one? Suppose
>> I have two value labels:
>>
>> label list numbers1
>>
>> 1 One
>> 2 Two
>> ...
>> 15 Fifteen
>>
>> label list numbers2
>>
>> 16 Sixteen
>> ...
>> 30 Thirty
>>
>> and I want to combine them into a single value label, numbers
>>
>> 1 One
>> 2 Two
>> ...
>> 30 Thirty
>>
>> The issue that motivates the previous question is the following: I am
>> appending two datasets with the same variable, which have a value
>> label with the same name attached. However, the labels are not the
>> same because they have different number-label assignments that
>> overlap, i.e
>>
>> use data1, clear
>> label list label1
>>
>> 1 One
>> 2 Two
>> 3 Three
>>
>> use data2, clear
>> label list label1
>>
>> 2 Two
>> 3 Three
>> 4 Four
>>
>> and I want to append the datasets, keep label1 attached to the
>> original variable and update label1 so it ends like
>>
>> label list label1
>>
>> 1 One
>> 2 Two
>> 3 Three
>> 4 Four
>>
>> Of course this should only work if the labels coincide for the numbers
>> that overlap. Is there a way to do this automatically?
>>
>

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