Announcement

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

  • dataex: exactly one array nonzero

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(A B)
      24    .
      24    .
      24    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
     240    .
     240    .
     240    .
     240    .
     240    .
     450  630
     450  630
     450  630
       .    .
       .    .
       .    .
       .    .
       .    .
       .    .
       .  180
       .  180
       .  180
      90  150
      90  150
      90  150
      90  150
     240  540
     240  540
     240  540
     240  540
     240  540
     240  540
      90  120
      90  120
      90  120
      90  120
      90  120
      90  120
       .    .
       .    .
       .    .
     600  600
     600  600
     600  600
       .    .
       .    .
     350    .
     350    .
     350    .
     350    .
     350    .
       .    .
       .    .
       .    .
       .    .
       .    .
     480  480
     480  480
     480  480
     480  480
     480  480
     480  480
     160    .
     160    .
     160    .
     160    .
       .    .
       .    .
       .    .
       .    .
     180  300
     180  300
     180  300
     180  300
       .  540
     150    .
     150    .
     150    .
       .  240
       .  240
       .  240
       . 1800
       . 1800
       . 1800
       . 1800
       . 1800
    1500 1800
    1500 1800
    1500 1800
    1500 1800
    end

    Command to generate a dummy

    if both A and B are non-zero (or non-missing value) then dummy is 1

    if exactly one of A and B is zero or missing value (and the other is nonzero or non-missing value) then dummy is 0
    Last edited by ajay pasi; 03 Nov 2022, 06:12.

  • #2
    You want an exclusive or operation. This is one way to do this. Note that this assumes you only care about whether the value is missing or not.

    Code:
    gen byte result = (!mi(A) & mi(B)) | (mi(A) & !mi(B))
    If instead you want the condition to be non-missing and not zero, you can modify it in this way.

    Code:
     gen byte result = ((A!=0 & !mi(A)) & (B==0 | mi(B))) | ((A==0 | mi(A)) & (B!=0 & !mi(B)))
    Last edited by Leonardo Guizzetti; 03 Nov 2022, 05:50.

    Comment


    • #3
      Thanks Leonardo.

      Comment


      • #4
        What do you want if both are missing or zero?

        Comment


        • #5
          Nick, if both are missing or zero, dummy should be missing value.

          Comment


          • #6
            Then you need different code. This is the simplest version I can think up.

            Code:
            gen count = (!missing(A) & (A != 0)) + (!missing(B) & (B != 0)) 
            
            gen wanted = count == 2 if count > 0

            Comment


            • #7
              Thanks Nick.

              Comment


              • #8
                This question was asked and already answered here: https://www.statalist.org/forums/for...3-egen-command

                Comment


                • #9

                  My apologies Hemanshu, just started using dataex, so posted twice ( one without, and the other with dataex). From now on, I shall post everything using dataex.

                  Comment

                  Working...
                  X