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
David Elliott <[email protected]>
To
[email protected]
Subject
Re: st: filter by value labels
Date
Thu, 31 Mar 2011 21:39:09 -0300
I was pursuing a similar Mata approach having used -st_vlload- in the
past for the same sort of task. However, Austin has beaten me to the
punch. I was actually considering using a view and creating an
indicator variable with the entire looping task completed in Mata.
The subsequent Stata command would simply be -command if indicator-,
or one could implement the conditional label selection in a prefix
command.
The real trick is how to pass a more complex if statement into the Mata routine.
David Elliott
Everything is theoretically impossible, until it is done.
Progress is made by lazy men looking for easier ways to do things.
-- Robert A. Heinlein (American science-fiction Writer, 1907-1988)
On 31 March 2011 18:00, Austin Nichols <[email protected]> wrote:
> 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/
>
*
* 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/