Bookmark and Share

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: filter by value labels


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   Re: st: filter by value labels
Date   Thu, 31 Mar 2011 17:00:02 -0400

Daniel Klein <[email protected]>:
I highly recommend Mata.  It will change your life.

But... there was
still a bug in my code--if
there was no match the old code would just drop a part of the condition.
Instead use (also adds an optional -if- qualifier):

prog labmatch, rclass
version 11.1
syntax anything [if] [, noList]
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=.)
mata:st_local("s",invtokens(strofreal(select(v,strmatch(t,"`m'"):>0)')))
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 `"`c'"'!=""&"`list'"=="" l `vs' `c'
return local cond `c'
return local vars `vs'
end
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index