Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Map logical operator across varlist?


From   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: Map logical operator across varlist?
Date   Wed, 17 Jan 2007 15:31:20 -0500

I don't think you can "map" an arbitrary operator.

But there are many ways to program your problem.

If you create a set of indicators as to whether the med variables are nonmissing (and by the way, a more generally correct test is ~mi() )...
gen byte nonmis_med1 = ~mi(med1)
gen byte nonmis_med2 = ~mi(med2)
etc., (and that can be made into a loop)

then if you have the trinary module, you can do

egen byte med_ok = rtvor(nonmis_*)

this will give you the row-or of the arguments. (The fact that it is 3-valued logic is irrelevant.)
Then you can
list med* if med_ok


To get trinary,
ssc inst trinary

I hope this helps.
--David


At 03:01 PM 1/17/2007, Joel J. Adamson wrote:

Does anyone know how to map a logical operation (e.g., | or &) across
a list so that I can save myself some typing?

Let me give you an example (the one that prompted the search for an
answer):

I want to list a set of variables, only if an observation  is not missing
for the variables in question.  We usually handle this by constructing
a small logical expression:

l med1 med2 if (med1~=.|med2~=.)

However, if I have six, or a hundred "med" variables (med*), it would
be easier (less error-prone) to type:

l med* if |(med*~=.)

Where the "|" before the "(" maps the operation onto the expanded list
of variables.

I know that Scheme (and hence probably S) have a function called "map"
that maps any operation onto a list, e.g.,

(map or '(1 2 3 ... ))

Does anyone know of a similar construct in Stata?  How about an egen
function that would accomplish the same goal?

Thanks,
Joel
--
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109
[...]
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index