Announcement

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

  • Generate a variable with IF AND

    I need to crate a variable with IF AND formula, but I did not find it in Stata. Could you please help me how I can create a variable with this conditions?
    Attached Files

  • #2
    Hi Anastasiya,

    you can use the following type of command:

    Code:
    Gen variable = value if var2==value & var3==value
    Best Rhys

    Comment


    • #3
      The equivalent to Excel's IF function
      Code:
      IF(x>y,"A","B")
      is Stata's cond function
      Code:
      cond(x>y,"A","B")
      The equivalent to Excel's AND function
      Code:
      AND(x>y,z>1)
      is Stata's logical and operator &
      Code:
      x>y & z>1

      Comment


      • #4
        Thank you a lot!

        I wrote this code:

        generate H = "A" if Y>= 1 & X >= 0.045
        replace H = "B" if Y <= 1 & X >= 0.045
        replace H = "C" if Y >= 1 & X <= 0.045
        replace H = "D" if Y <= 1 & X <= 0.045

        Y is crises variable, X is threshold.

        But I need to write code, that if sum of Y Variable within the next 24 months is more (less) than 1, then .....
        In Excel we can use sum of count using a loop or just sum(A1:A24). I need to have something like in Stata and place it in the code (sum of 24 Months of Y>= 1). Could you kindly help me how to figure out in Stata please.
        Last edited by Anastasiya Lu; 24 Apr 2021, 15:21.

        Comment


        • #5
          #4 is superseded by a new thread at https://www.statalist.org/forums/for...cells-a1-a24-0

          Comment

          Working...
          X