Announcement

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

  • How to delete specific observations?

    I have 3 variables that I want to construct an aggregate variable with:
    racfew
    rachaf
    racmost

    I have (.i) for some (missing values)

    The new variable I want to construct is Racpeers= racfew + rachaf + racmost

    How can I have Racpeers = .i (missing) only if all three variables (racfew, rachaf and racmost) are .i? If say one or two of the three is missing I want to assign .i= 0.

    In other words, say I have a value of 2 for rachaf and .i for both racfew and racmost, I want Racpeers = 2.

    But if I have racfew, rachaf and racmost = .i , I want to have Racpeers = .i or drop the observation for that variable.

    How can this be done?






  • #2
    Code:
    egen racpeers = rowtotal(racfew rachaf racmost), missing

    Comment


    • #3
      Thank you, Clyde Schechter, but unforunately this still gives me the issue that i was talking about. It counts .i as 0 so racpeers is 0 when rachaf= .i and racfew = .i and racmost = .i. The only time I don't want racpeers to be 0, but instead missing, is when all three: racfew, rachaf and racmost are all missing.

      Comment


      • #4
        The desire isn't very clear. You ask in the title about deleting observations and in the text about creating a new variable.

        As I understand it you want

        .i if all three variables are .i (missing)

        0 if one or two are missing

        the sum of the non-missing values otherwise

        Code:
        gen countdoti = (racfew == .i) + (rachaf == .i) + (racmost == .i)
        gen racpeers  = cond(count == 3, .i, cond(count >= 1, 0, max(racfew, 0) + max(rachaf, 0) + max(racmost, 0)))
        Last edited by Nick Cox; 17 Mar 2020, 06:26.

        Comment


        • #5
          Re: #3. That shouldn't be happening. With the -missing- observation specified, you should get missing values when all three are missing, but missings counted as zeroes otherwise. Please use the -dataex- command to show example data that exhibits this problem, and show the exact code you used. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

          Comment


          • #6
            Thank you for the responses. Clyde Schechter, apologies everything is right as you said! There are just inconsistencies with responses (weirdness with the data collected), hence I am getting weird numbers and got confused, but the methodology is correct!

            Comment

            Working...
            X