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   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: filter by value labels
Date   Fri, 1 Apr 2011 11:42:20 +0100

In very limited testing I haven't reproduced your first problem.
-syntax- should be indifferent to whether the comma is preceded by a
space.

The second problem can be addressed using this kind of technique often
used in Stata's own commands.

foreach w of local show {
        local length = length("`w'")
        if "`w'" == substr("list", 1, `length') {
                  /// list is requested; act accordingly
       }
       if "`w'" == substr("tabulate", 1, `length') {
                  /// tabulate is requested; act accordingly
       }
}

That way, you allow any abbreviation of "list" and "tabulate". If you
want to insist on "ta" as minimal, you could do that. For examples,
-viewsource duplicates.ado-.

It's your program as you are running with it, but some of the things
to develop and strengthen it could include

1. Support for -in-.

2. Trapping calls in which the variables don't have value labels attached.

3. Trapping cases in which the user departs from the pairing of
variable names and stuff to be matched by forgetting one or the other.
E.g. you could test (partially) for odd numbers of words in
`anything'.

4. You could (optionally) echo what the user is asking for in less terse form.

Nick

On Fri, Apr 1, 2011 at 11:08 AM,  <[email protected]> wrote:

> 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=.)
> 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 "`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'
> 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