Announcement

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

  • If double equal sign with multiple conditions

    I am trying to generate a variable y that counts the number of times that my conditions are satisfied within each country-year:

    egen y=count(targtype1) if targtype1==14 & nkill>0 & nkill !=. | targtype1==14 & nwound>0 & nwound !=., by(country_txt iyear)

    So i want to count observations where either targettype1=14, nkill>0, and nkill !=. or where targtype1=14, nwound>0, and nwound!=. Directly after the if, i use a double equal sign in targtype1==14 because i am testing whether the statement "targtype1 equals 14" is true. But in the proceeding conditions, I use a single equal sign. Is this correct, or should I use a double equal sign in every case? The code runs and seems to be working as is, but I want to be sure that I am not making a mistake.

    disclaimer: i am fairly new to stata

    Thanks!
    Last edited by Julian Duggan; 27 Jul 2016, 11:58.

  • #2
    Hello Julian,

    Welcome to the Stata Forum!

    The double equal sign was typed correctly. And the use of "=" after "!" as well. You could also "experiment" with ">=" and "<=" so as to get the gist.

    Best,

    Marcos
    Last edited by Marcos Almeida; 27 Jul 2016, 12:29.
    Best regards,

    Marcos

    Comment


    • #3
      You might find using some parentheses would make it easier to follow the logic of your conditions.

      Comment


      • #4
        Hello Julian. You've got targettype1==14 on both sides of your OR, so I think you can simplify things a bit, as follows (untested):

        Code:
        egen y=count(targtype1) if targtype1==14 & ((nkill>0 & nkill !=.) | (nwound>0 & nwound !=.)), by(country_txt iyear)
        With only 3 conditions, this is still relatively easy to follow. But when I have an IF involving several conditions, I find it helpful to compute a bunch of FLAG variables for the various conditions, and then use those FLAGS in the IF statement. Something like this:

        Code:
        // Generate 3 flag variables for the conditions of interest:
        generate tt14 = targtype1==14             // Flag for condition targtype1==14
        generate nk_valid = nkill>0 & nkill <.    // Flag for valid within range value of nkill
        generate nw_valid = nwound>0 & nwound <.  // Flag for valid within range value of nwound
        
        egen y=count(targtype1) if tt14 &  (nk_valid | nw_valid), by(country_txt iyear)
        drop tt14 nk_valid nw_valid  // Get rid of the Flag variables if they are no longer needed
        Admittedly, this may not be the tightest code possible; but you may find it less confusing and easier to maintain when there are several conditions. Notice that it has reduced the number of layers of parentheses that are required.

        HTH.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 19.5 (Windows)

        Comment


        • #5
          Thanks all!

          Comment

          Working...
          X