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]

st: RE: or operator with lists of variables


From   Joe Canner <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: or operator with lists of variables
Date   Mon, 13 Jan 2014 22:03:41 +0000

Philipp,

The brute force way to do it is:

gen identical=0
forvalues i=1/20 {
forvalues j=1/25 {
  replace identical=1 if base`i'==var`j'
}
}

If you have a lot of observations, this may not be very efficient, so you might want to use the -inlist- function:

gen identical=0
forvalues i=1/20 {
  replace identical=1 if inlist(base`i',var1,var2,var3,...,var25)
}
}

If var1-var25 are strings instead of numerics you will have to split it into three function calls:

replace identical=1 if inlist(base`i',var1,...,var9) | inlist(base`i',var10,...,var18) | inlist(base`i',var19,var25)

Regards,
Joe Canner
Johns Hopkins University School of Medicine
________________________________________
From: [email protected] [[email protected]] on behalf of Loew, Philipp [[email protected]]
Sent: Monday, January 13, 2014 4:50 PM
To: [email protected]
Subject: st: or operator with lists of variables

Dear Statalists,

I try to solve the following problem:
- I know what to to in a simple case, e.g.:

gen identical=.
replace identical=1 if base1==variable1 | base1==variable2 | base1==variable3

- however, I would like to compare two lists of variables (base1-base20 and var1-var25) with the "or operator" (I try to
describe it in "pseudo-code"):

gen identical=.
replace identical=1 if (base1-base20) == (var1-var25)

- In this case the condition should be satisfied if any of the base and var combinations has identical values.

Thank you!

Kind regards
Philipp

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index