Announcement

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

  • For loop to create variables based on conditions

    Dear Statalist members,

    this is my first post here and I am quite new to Stata so please don't be too harsh on me haha.
    I would like to create some variables based on a conditions, I have a panel dataset with T=20 and N=19 concerning the member states of the Eurozone, I am employing a panel data threshold methodology and I have already estimated my model but due to misspecification problems I am thinking about different estimates, thus I am thinking about creating the regime-dependent coefficients manually and run for instance a regression with PCSE.
    My piece of code is the following but it does not work, any good recommendations/advice would be appreciated.

    #the three regimes
    by Country: gen q1=0
    by Country: gen q2=0
    by Country: gen q3=0

    # a for loop


    Code:
    foreach x in gross_debt_L1{
                  replace q1=gross_debt_L1  if gross_debt_L1<=95.782
                  replace q2=gross_debt_L1  if gross_debt_L1<=95.782 & gross_debt_L1 <=120.038
                  replace q3=gross_debt_L1  if gross_debt_L1>120.038
    }
    Any


  • #2
    don't think you need a loop. Not using the x for anything.

    and you are simply assigning a known variable value to new variables. do you want the mean?

    Comment


    • #3
      As George Ford implies the code boils down to

      Code:
      gen q1=gross_debt_L1  if gross_debt_L1<=95.782  
      
      gen q2=gross_debt_L1  if gross_debt_L1<=95.782 & gross_debt_L1 <=120.038  
      
      gen q3=gross_debt_L1  if gross_debt_L1>120.038
      unless you really do need zeros rather than missings, which you could get with

      Code:
      mvencode q1 q2 q3, mv(0)
      You're trying to put values of 95.782 in two classes, which probably won't bite, but it is a little inconsistent. More importanly, I imagine that your code should be

      Code:
      gen q2=gross_debt_L1 if gross_debt_L1>95.782 & gross_debt_L1 <=120.038
      Last edited by Nick Cox; 09 Jan 2023, 11:03.

      Comment


      • #4
        Nick Cox and George Ford thank you very much for your insight, I really do need zeros because a zero means that the regime dependent coefficient is zero since its value is assigned based on an Indicator function I(q_i_t<γ).
        This last piece of code is actually very helpful

        Comment

        Working...
        X