Announcement

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

  • Looping within a loop

    Hello,
    I am using a panel dataset. It both id and ic variable (ic: income category: HI, LI, LMI, UMI)
    I want to write a loop that performs a dickeyfuller test for each id in ic. The following table summerizes the data:

    ic id
    HI 1 to 42
    LI 43 to 61
    LMI 62 to 86
    UMI 87 to 118

    The loop is as follows:
    use si1981, clear
    sort ic cc year
    egen id1=group(ic cc)
    encode ic, gen(ic1)

    foreach i of numlist 1/4 {
    foreach j of numlist 1/42 43/61 62/86 87/118 {
    tab ic id if id1==`i'
    dfuller gcf, drift lags(4), if ic1==`i' & id1==`j'
    }
    }
    *

    The above code works for first ic (i.e., HI). I want the double-loop to run the test for all ids in each ic.
    I am a beginner in looping. I was wondering if local or global can help me out.

    Thank you in advance.
    Last edited by Santosh Dash; 23 Feb 2019, 19:55.

  • #2
    You include a variable cc which you do not describe. You generate id1 as a grouping of ic and cc, yet your tab command suggests it can only take the values 1-4, which suggests cc is constant. So I can't completely figure out what you are doing.

    With that said, the following shows one way to loop across the four values of ic, and for each value of ic, loop across the corresponding values of id.
    Code:
    local n_HI 1/42
    local n_LI 43/61
    local n_LMI 62/86
    local n_UMI 87/118
    
    foreach ic in HI LI LMI UMI {
        display "value of ic is `ic'"
        display "value of id is " _continue
        forvalues id = `n_`ic'' {
            display "`id' " _continue
        }
        display
    }
    Code:
    value of ic is HI
    value of id is 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 
    value of ic is LI
    value of id is 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 
    value of ic is LMI
    value of id is 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 
    value of ic is UMI
    value of id is 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    With that demonstration out of the way, the code you will want will be something like
    Code:
    local n_HI 1/42
    local n_LI 43/61
    local n_LMI 62/86
    local n_UMI 87/118
    
    foreach ic in HI LI LMI UMI {
        forvalues id = `n_`ic'' {
            dfuller gcf if ic=="`ic'" & id==`id'  & cc=I don't know what you need here, drift lags(4)
        }
    }

    Comment


    • #3
      Hello Sir,
      Thank you for your reply. I am extremely sorry for not explaining the variable cc which is the World Bank country code. The cc variable in the egen function helps generate new id, id1 which assigns id for each ic.

      The code is worked for me. Thank you a lot, Sir
      Last edited by Santosh Dash; 24 Feb 2019, 23:42.

      Comment

      Working...
      X