Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: filter by value labels

From   <>
To   <>
Subject   Re: st: filter by value labels
Date   Fri, 1 Apr 2011 12:08:30 +0200

Thank you all very much for...better: forval(uable) discussion on this subject and esp. Austin for providing a solution.

Works great!!

Stefan Gawrich

I extended the ado-file with some basic output features to meet my own purposes.
(E.g. I want a list of unit IDs as a result of my value label query)
I document it here but I'm no Ado-Programmer so this is unqualified and without guarantee:

I added three options:
other: to specify other vars (like the unit ID) in the list or tabulate command

show : to specify if the results should be displayed as list (show(l)) or tab (show(t)) or both (show(l t))

tvars: to specify a maximum of 2 variables to be displayed in one-way or two-way tab. 
If tvars is not specified: the tab uses the first "other var" and the first "command var".
If option "other" is not specified: the first "command var" is tabulated one-way.

There are some pitfalls left:

- e.g. labmatch2 var1 *test*, other(...
with no space before the comma results in an error

- show(list) or show(tabulate) would both display a list and a tab because I only check for "*l*" and "*t*"


prog labmatch2, rclass
version 11.1
syntax anything [if] [, other(varlist) show(namelist) tvars(varlist max = 2) ]
if `"`if'"'=="" loc c "if "
else loc c `if'
forval j = 1(2)`: word count `anything'' {
loc v: word `j' of `anything'
loc vs `vs' `v'
loc m: word `=`j'+1' of `0'
mata:st_vlload("`:val lab `v''",v=.,t=.)
tokenize `s'
if `"`1'"' !="" {
 if `"`c'"'!="if " loc c `c'&
 if `"`2'"'=="" {
  loc c `c'(`v'==`1')
 else {
  loc c `c'(
  loc n 1
  while `"`1'"'!="" {
   if `n++'==1 loc c `c'(`v'==`1')
   else loc c `c'|(`v'==`1')
   mac sh
  loc c `c')
else {
 di as err `"value "`m'" does not appear in label for `v' "'
 exit 111
if `"`c'"'=="if " loc c
if "`show'" == "" | strmatch("`show'" , "*l*") {
if `"`c'"'!=""&"`list'"=="" l `vs' `other' `c' 
if strmatch("`show'" , "*t*")  {
if "`tvars'" == "" {
local firstother : word 1 of `other'
local firstanything : word 1 of `anything'
local tabvars "`firstother' `firstanything'" 
if `"`c'"'!="" tab `tvars'  `c'

return local cond `c'
return local vars `vs'

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index