Announcement

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

  • cond() function : how to generate a dummy variable, but not put == 1 to missing values?

    Hi,

    I want to create a variable using -cond()- as follows:

    Code:
    gen tariff_2_more_15000_w = cond(tariff_2 == 1 & (power_p1 <=15000 | power_p2 <=15000), 1,0)
    replace tariff_2_more_15000_w = 0 if tariff_2 == 1 & (power_p1 >15000 | power_p2 >15000)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(power_p1 power_p2) float(tariff_2_more_15000_w tariff_2)
     3300     . 0 1
     3450  3450 1 1
     2300  2300 1 1
     3450  3450 1 1
     3450  3450 1 1
     2300  2300 1 1
     5400  5400 1 1
     3300  3300 1 1
     3450     . 0 1
     2300  2300 1 1
     1725     . 0 1
     3450  3450 1 1
     3450  3450 1 1
     3100  3100 1 1
     1000  1000 1 1
     3300  3300 1 1
     2000  2000 1 1
     2300  2300 1 1
     3450  3450 1 1
     3450  3450 1 1
     4600  4600 1 1
     1725  1725 1 1
     3450     . 0 1
        .     . 0 1
     2300  2300 1 1
     3100  3100 1 1
     3450  3450 1 1
     6200  6200 1 1
     2200  2200 1 1
     2300  2300 1 1
     2000  2000 1 1
     3500  3500 1 1
     2300  2300 1 1
     2300  2300 1 1
     2300  2300 1 1
     2300  2300 1 1
     2300  2300 1 1
     2300  2300 1 1
     3300  3300 1 1
     3450  3450 1 1
     3450  3450 1 1
     3450  3450 1 1
     3450  3450 1 1
     1150  1150 1 1
     5500  5500 1 1
     2300  2300 1 1
     3450  3450 1 1
     3450     . 0 1
     3450  3450 1 1
     2300  2300 1 1
     3100  3100 1 1
     3450  3450 1 1
     3450     . 0 1
     3450  3450 1 1
     3450  3450 1 1
     3100  3100 1 1
     3450  3450 1 1
     5500  5500 1 1
     3300  3300 1 1
     3300  3300 1 1
     2300  2300 1 1
     4000  4000 1 1
     2200     . 0 1
     2300  2300 1 1
     3500  3500 1 1
     3450  3450 1 1
     3450  3450 1 1
     3100  3100 1 1
     3400     . 0 1
     3450  3450 1 1
     2800  2800 1 1
     3450  3450 1 1
     3450  3450 1 1
     2300  2300 1 1
     2000  2000 1 1
     3100  3100 1 1
     2300  2300 1 1
    13200 13200 1 1
     3450  3450 1 1
     2200     . 0 1
     3450  3450 1 1
     3300  3300 1 1
     2300  2300 1 1
     6200  6200 1 1
     2300  2300 1 1
     3300  3300 1 1
     3400  3400 1 1
     2300  2300 1 1
     2300  2300 1 1
     3450  3450 1 1
     2300  2300 1 1
     3450  3450 1 1
     3450     . 0 1
     3100  3100 1 1
     3450     . 0 1
     3450  3450 1 1
     2300  2300 1 1
     3000  3000 1 1
     3450  3450 1 1
     3450     . 0 1
    end


    However, the new variable contains also a dummy equals to one when both power_p1 and power_p2 are missing, which I don't want.
    Basically, if one of variable power_p1 or power_p2 is missing, it should contain 1.

    But, if both are missing, it should take the value of 0, instead of 1 right now.

    Thank you for your help.
    Best,

    Michael
    Last edited by Michael Duarte Goncalves; 27 Sep 2023, 09:34.

  • #2
    However, the new variable contains also a dummy equals to one when both power_p1 and power_p2 are missing, which I don't want.
    I don't think so. Check your example again. If either power_p1 or power_p2 is missing, the expression evaluates to zero. I think this is what you want:

    Code:
    replace tariff_2_more_15000_w = (missing(power_p1) | missing(power_p2)) & !(missing(power_p1) & missing(power_p2)) ///
                                                           if tariff_2 == 1 & (missing(power_p1) | missing(power_p2))

    Comment


    • #3
      Don't these two lines:

      Code:
      gen tariff_2_more_15000_w = cond(tariff_2 == 1 & (power_p1 <=15000 | power_p2 <=15000), 1,0)
      replace tariff_2_more_15000_w = 0 if tariff_2 == 1 & (power_p1 >15000 | power_p2 >15000)
      Turn out to be equivalent to this line?

      Code:
      gen tariff_2_more_15000_w = tariff_2 == 1 & (power_p1 <=15000 & power_p2 <=15000)
      Last edited by Daniel Schaefer; 27 Sep 2023, 11:04.

      Comment


      • #4
        Hi Daniel Schaefer,

        It works well, thanks for the suggestion.
        However, I would like to have for my variable tariff_2_more_15000_w a "." in lieu of 0 when both power_p1 and power_p2 are missing.

        Here is a -dataex- example:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long(power_p1 power_p2) float(tariff_2 tariff_2_more_15000_w)
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
        345000 345000 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
             .      . 1 0
        end
        Thank you in advance.
        Best,
        Michael

        Comment


        • #5
          There are a few ways to do this, but the simplest in my mind is just:

          Code:
          replace tariff_2_more_15000_w = . if missing(power_p1) & missing(power_p2)

          Comment


          • #6
            Hi Daniel Schaefer,

            Thank you for the suggestion. It is straightforward.

            Best,

            Michael

            Comment

            Working...
            X