# RE: st: Map logical operator across varlist?

 From "Nick Cox" <[email protected]> To <[email protected]> Subject RE: st: Map logical operator across varlist? Date Wed, 17 Jan 2007 21:24:59 -0000

```Understanding this hinges rather delicately on
knowing how Stata works. Scott commendably didn't underline
the way in which his solution is smart but
I will do that for him. The principle is the
same whatever the number of variables so
I will simplify to three,

x1 x2 x3

which Scott has constructed as partly missing.

Let's pick up the story with

unab a : x*

This expands the wild card -x*- and puts
the result, which in our case will be

x1 x2 x3

into the local macro -a-. Naturally this is trivial
with just three names, but the point is that you could
the same thing with no more typing and very many more
variables in -x*-.

Now

local b: subinstr local a " " " != . & ", all

says

substitute in the string contained in the local macro -a-
in the following way: take every space and replace it by
" != . & "
and put the result into local macro -b-. The double
quotes are delimiters in every instance and not part of
the strings being specified.

So, let's do that by hand: There are two spaces, one
between "x1" and "x2" and one between "x2" and "x3"
so that after that local macro -b- contains

x1 != . & x2 != . & x3

We have to know here that -unab- does not
of finding that out....

Now as his final flourish Scott writes

l x* if `b' != .

Now what happens next is a strict one-two:

One, Stata sees a local macro name there and
substitutes the contents of the local macro in
the same place. So the command now reads

l x* if x1 != . & x2 != . & x3 != .

You see what has happened? Scott has arranged things
so the the -if- condition ends with

!= .

as it should.

Two, that command is what -list- sees, and will
execute.

Quite separately of all this the code could be tweaked to
cope with extended missings .a to .z, but that's easily
done, say by changing != to <.

Nick
[email protected]

[email protected]

> You might be able to use the extended macro function -subinstr- and -
> unab-
>
> For example:
> clear
> set obs 100
> set seed 1234
> forv i = 1/10 {
> 	qui gen x`i' = uniform()
> 	qui replace x`i' = . if uniform()<.2
> }
>
> unab a: x*
> local b: subinstr local a " " " != . & ", all
> disp "`b'"
> l x* if `b' != .

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```