Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help egen anyvalue with another variable instead of a integer list.

    In a datasets with two variables, Var1 and Var2, how can I drop the records that have a Var1 value different to any Var2?

    I thought something like a egen with anyvalue, but the ¨integer list¨ is composed by all the values of Var2.

    Thanks

  • #2
    Welcome to Statalist. Could you elaborate this a bit more, and preferably with an example?

    What does it mean by "any Var2"? Do you mean:

    1) Var 2 can have values, for example, 1, 2, 3, 4, 5; and if Var 1 is not any of these these 5 values, drop the case.

    or

    2) Drop the case if Var 1 is different that Var 2?

    Please take a moment to read the FAQ (http://www.statalist.org/forums/help) and check out how to use dataex to provide an example. That'd speed up the exchanges.

    Comment


    • #3
      Thanks Ken

      I think the case would be the the first one 1): Var 2 can have values, for example, 1, 2, 3, 4, 5; and if Var 1 is not any of these these 5 values, drop the case.

      clear
      input float ID long stub1
      1 .
      2 .
      3 .
      4 .
      5 .
      6 .
      7 2
      8 5
      9 7
      10 .
      end

      In this example, I'd like to keep the records with ID 2 5 and 7 and drop the rest.

      Comment


      • #4
        Code:
        frame put stub1, into(to_match)
        frame to_match {
            drop if missing(stub1)
            duplicates drop
            isid stub1, sort
        }
        
        frlink m:1 ID, frame(to_match stub1)
        keep if !missing(to_match)
        drop to_match
        frame drop to_match

        Comment


        • #5
          Thank you very much Clyde! That was very helpful.

          And how can I make Stata keep not only ID 2, 5 and 7, but also 8 and 9 of the example?
          That would be, to drop the records that both have
          1) ID not included in any of the stub1 values
          2) stub1 ==.

          Thank you!

          Comment


          • #6
            Change -keep if !missing(to_match)- to -keep if !missing(to_match) | !missing(stub1)-.

            Comment

            Working...
            X