Announcement

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

  • Generate New Variable under certain conditions

    Hello,

    I am trying to generate a new variable that gives for all Years and NA's the value of the IV_Exportshock under NA "AZ" in the Year 2002.

    I tried:

    Code:
     gen IV_Imortshock_AZ2002 = IV_Importshock if NA=="AZ" & Year==2007
    but as it can be expected it only gives the value for the year 2007 under NA "AZ" but I would like the newly created variable to display the same value (e.g. AZ Import shock in 2002) in all rows.

    Do you have any suggestions on how to adjust the command? Thank you very much in advance!

    Code:
    clear
    input float Year str2 NA float IV_Exportshock
    2002 "AZ"             .
    2007 "AZ"  233546645504
    2012 "AZ" -232296038400
    2017 "AZ"   28940210176
    2002 "C1"             .
    2007 "C1"   36097335296
    2012 "C1"  -41467588608
    2017 "C1"     329670976
    2002 "C2"             .
    2007 "C2"  154366148608
    2012 "C2"   73867247616
    2017 "C2"  -37996838912
    2002 "C3"             .
    2007 "C3"   11276726272
    2012 "C3"   60813754368
    2017 "C3"   44920815616
    2002 "C4"             .
    2007 "C4"  -31695208448
    2012 "C4"  120318328832
    2017 "C4" -1.258267e+11
    2002 "C5"             .
    2007 "C5"   18995791872
    2012 "C5"   -3796148224
    2017 "C5"    9838895104
    2002 "DE"             .
    2007 "DE"   -9318644736
    2012 "DE"   22979246080
    2017 "DE"  -23000590336
    2002 "JZ"             .
    2007 "JZ"  -13804761088
    2012 "JZ"  -42036465664
    2017 "JZ"   74112778240
    2002 "MN"             .
    2007 "MN"      91377008
    2012 "MN"   46314561536
    2017 "MN"  161840463872
    2002 "RU"             .
    2007 "RU"   -2540648960
    2012 "RU"  225284472832
    2017 "RU" -1.945715e+11
    end

  • #2
    Lea, could you please give some concrete examples about the new variable? For example, what should the values of the new variable be for AZ-2002, AZ-2007, C1-2002, CI-2017, etc.

    Comment


    • #3
      Thank you very much for your quick response!
      Below I tried to make it more clear. I want to generate a new variable that has the value of the AZ 2007 IV Exportshock for all rows. I hope it is more clear what I am looking for now. Thank you in advance!

      Code:
      clear
      input float Year str2 NA float IV_Exportshock AZ2007 
      2002 "AZ"             .                                          22369271808
      2007 "AZ"   22369271808                               22369271808
      2012 "AZ"  207609036800                              22369271808
      2017 "AZ" -2.302268e+11                                ....
      2002 "C1"             .                                           ....
      2007 "C1"   -1878561408
      2012 "C1"     -21604408                                 22369271808
      2017 "C1"  1.921367e+11
      2002 "C2"             .
      2007 "C2"      15662273
      2012 "C2"   18800269312
      2017 "C2"   11374891008
      2002 "C3"             .
      2007 "C3"   38659530752
      2012 "C3"  -4.10252e+10
      2017 "C3"  207013560320
      2002 "C4"             .
      2007 "C4"    1190220288
      2012 "C4"   -6408663040
      2017 "C4"   28415473664
      2002 "C5"             .
      2007 "C5"  -5.50301e+10
      2012 "C5"   7.12828e+10
      2017 "C5"    2018844672
      2002 "DE"             .
      2007 "DE"   32220823552
      2012 "DE"   8.54112e+10
      2017 "DE"   -9712238592
      2002 "JZ"             .
      2007 "JZ"    7928404480
      2012 "JZ"  2.148154e+11
      2017 "JZ" -1.945715e+11
      2002 "MN"             .
      2007 "MN"  195203432448
      2012 "MN" -187122941952
      2017 "MN"  -46006493184
      2002 "RU"             .
      2007 "RU"  1.563361e+11
      2012 "RU" -133358952448
      2017 "RU"  -21855260672
      end

      Comment


      • #4
        Thanks for the clarification. Below please find the code.

        Code:
        sum IV_Exportshock if NA == "AZ" & Year == 2007
        gen AZ2007 = r(mean)
        There is another one-line solution as below.

        Code:
        egen AZ2007 = total((NA=="AZ" & Year==2007)*IV_Exportshock)
        Last edited by Fei Wang; 23 Jun 2022, 05:55.

        Comment


        • #5
          Thank you very much Fei, that works perfectly!

          Comment

          Working...
          X