Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: create a variable based on a recurring value in a varlist


From   Seb Buechte <sfbuechte@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: create a variable based on a recurring value in a varlist
Date   Thu, 13 Oct 2005 09:01:59 +0200

Derek,

you could take a "brute force" approach by comparing each var with all
the other vars using two loops:

gen interlock=0
foreach var1 of varlist PID1 PID2 .... {
    foreach var2 of varlist PID2 PID3.... {
        if "`var1'"!="`var2'" { // making sure you do not compare the
var with itself
           replace interlock=1 if `var1' == `var2'
        }
    }
}

I am not too sure how long it will take to run through these loops.

Kind regards,
sebastian


On 10/13/05, Derek Darves <darves@uoregon.edu> wrote:
> Hi all,
>
> I have a group of variables:
>
> PID1 - PID15
>
> PID* takes on values from 1 to 8000, and many are missing.
>
> Basically, I would like to make a new variable, called interlock,
> that is equal to 1 if any of the variables in the list are equal to
> any other variable in the list (not including itself, of course).
> For example, if PID5==705 and PID14==705 I would like like interlock==1
>
> Likewise, if none of the the variables in PID* take on the value of
> any of the other variables in PID*, I would like interlock==0
>
> I tried this:
> egen interlock = group(pid1_a  pid1_b  pid2_a  pid2_b  pid3_a
> pid3_b  pid4_a pid4_b  pid5_a  pid5_b  pid6_a  pid6_b  pid7_a
> pid7_b  pid8_a  pid8_b  pid9_a  pid9_b  pid10_a  pid10_b  pid11_a
> pid11_b  pid12_a  pid12_b  pid13_a  pid13_b  pid14_a  pid14_b   pid15_a)
>
> , but it returned all missing values when I know that some share a
> common value in two of the PID* fields.
>
> Lastly, not that it should matter, but the above is a simplifying
> example. In my actual dataset I have about 130 PID* variables. I just
> mention this in case I am hitting some kind of memory limitation (I
> am not receiving any errors when I run the command, though, it just
> doesn't work).
>
> Thanks in advance,
>
> Derek
>
> *
> *   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/
>


--
- Seb F Buechte
-
- Stay tuned!

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index