Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Combining value labels, appending
From
Jorge Eduardo Pérez Pérez <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Combining value labels, appending
Date
Thu, 12 May 2011 14:49:56 -0400
Thank you both for your solutions. This would definitely make a nice
addition to the -labutils- package.
_______________________
Jorge Eduardo Pérez Pérez
On Thu, May 12, 2011 at 6:36 AM, daniel klein
<[email protected]> wrote:
> Nick was obviously qicker, but here's my try (-labvalpool-) anyway.
>
> Syntax is
>
> labvalpool newlblname lbname1 lblname2 [lblname3 ... lblnamek] [
> ,REPLACE UPDATE Pool]
>
> newlabelname specifies the value label name for pooled labels
> lblname1 ... lblnamek are names of (sets of) value labels to be pooled
> (at least two must be specified)
>
> REPLACE allows newlblname to be replaced in case it is already defined
> UPDATE overwrites definitions in the order of lblnames specified.
> Later definitions overwrite erlier ones.
> Pool pools value labels
>
> Note that Stata 11.2 is required, because -label copy- is used.
>
> ---program---
>
> *! version 1.0.0 12may2011 Daniel Klein
>
> prog labvalpool
> vers 11.2
>
> syntax namelist(id = "newlblname lblnames" min = 2) ///
> [, REPLACE UPDATE Pool]
>
> /*check options
> ----------------*/
> if "`update'" != "" & "`pool'" != "" {
> di "{err}update and pool not both allowed"
> exit 198
> }
> else loc skip = ("`update'" == "") & ("`pool'" == "")
>
> /*get label names
> ------------------*/
> loc newlbl : word 1 of `namelist'
> if "`replace'" == "" {
> cap as `: word count `namelist'' > 2
> if _rc {
> di "{err}too few value labelnames specified"
> exit 198
> }
> cap la li `newlbl'
> if !_rc {
> di "{err}label `newlbl' already defined"
> exit 110
> }
> }
> loc namelist : list namelist - newlbl
> loc nlbl : word count `namelist'
>
> /*check value labels exist and get names and values
> ----------------------------------------------------*/
> tempname vls
> forval j = 1/`nlbl' {
> loc lblnam`j' : word `j' of `namelist'
> qui la li `lblnam`j''
> mata : st_vlload("`lblnam`j''", vls = ., txt = .)
> mata : st_matrix("`vls'" ,vls)
> forval k = 1/`= rowsof(`vls')' {
> loc val = `vls'[`k', 1]
> loc lblval`j' `lblval`j'' `val'
> }
> }
>
> /*pool label sets
> ------------------*/
> loc lblvalpool `lblval1'
> la copy `lblnam1' `newlbl' ,`replace'
> forval j = 2/`nlbl' {
> foreach v of loc lblval`j' {
> if `: list posof "`v'" in lblvalpool' {
> if `skip' continue
> else if "`pool'" != "" {
> la de `newlbl' `v' ///
> `"`: lab `newlbl' `v'' `: lab `lblnam`j'' `v''"' ,modify
> continue
> }
> }
> la de `newlbl' `v' `"`: lab `lblnam`j'' `v''"' ,modify
> loc lblvalpool `lblvalpool' `v'
> }
> }
> end
>
> --end---
>
> ---example---
>
> label define label1 1 "one" ,modify
> label define label1 2 "two" ,modify
> label define label1 3 "three" ,modify
> label define label1 4 "four" ,modify
>
> label define label2 4 "double" ,modify
> label define label2 5 "five" ,modify
> label define label2 6 "six" ,modify
> label define label2 7 "seven" ,modify
> label define label2 8 "eight" ,modify
>
> label define label3 3 "new three" ,modify
> label define label3 4 "antoher four" ,modify
> label define label3 8 "new eight" ,modify
> label define label3 9 "nine" ,modify
>
> la li
>
> labvalpool newlabel label1 label2 label3
> la li
>
> labvalpool newlabel label1 label2 label3 ,replace update
> la li
>
> labvalpool newlabel label1 label2 label3 ,replace pool
> la li
>
> ---end---
>
> Daniel
> *
> * 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/
>
*
* 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/