Announcement

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

  • calculate growth rate in panel data

    Hi,

    In Stata IC 13.0 I have panel of branch level bank data and US state-level gdp. The data essentially looks like:

    Bank date statenum gdp
    A 2002q1 MA 100
    B 2002q1 MA 100
    C 2002q2 MA 110
    D 2002q2 MA 110
    E 2002q1 ME 95
    F 2002q1 ME 95
    G 2002q2 ME 97
    H 2002q2 ME 97


    I want to create a variable for state level gdp growth, but get the error "not sorted" when I use the following:

    Code:
    encode state, g(statenum)
    sort date lgdp
    bys statenum: g grgdp=D.gdp

    Any advice would be greatly appreciated!

  • #2
    The gdp data is state-level, but you have multiple observations for the same state in the same quarter, with different banks. So, it seems the banks themselves have nothing to do with this. Do I have that right?

    If that's correct, I would first reduce to a data set with just states, quarters and gdp, eliminating duplicate observations. Then calculate growth, then merge back to the original data.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 bank float date str2 state int gdp
    "A" 168 "MA" 100
    "B" 168 "MA" 100
    "C" 169 "MA" 110
    "D" 169 "MA" 110
    "E" 168 "ME"  95
    "F" 168 "ME"  95
    "G" 169 "ME"  97
    "H" 169 "ME"  97
    end
    format %tq date
    
    //    CREATE NUMERIC STATE VARIABLE
    encode state, gen(num_state)
    
    //    REDUCE DATA TO STATES, DATES, AND GDP
    preserve
    drop bank
    
    // ASSURE GDP IS CONSISTENT FOR ALL OBSERVATIONS ON A GIVEN
    // STATE IN A GIVEN QUARTEER
    by num_state date (gdp), sort: assert gdp[1] == gdp[_N]
    
    // ELMINATE DUPLICATE OBSERVATIONS
    duplicates drop
    
    // CALCULATE GROWTH RATES & SAVE IN SEPARATE TEMPORARY FILE
    xtset num_state date
    gen gdp_growth = F1.gdp - gdp
    gen gdp_growth_rate = gdp_growth/gdp
    tempfile growth
    save `growth'
    
    //    BRING BACK ORIGINAL DATA
    restore
    
    //  MERGE IN GROWTH INFORMATION
    merge m:1 num_state date using `growth'
    Note: this requires that your date variable be an actual numeric Stata internal quarterly date, not a human readable string variable that looks like a quarterly date. If your data are not that way, apply the -quarterly()- function. (See -help quarterly()-.)

    Your original error message, "not sorted", arose because you attempted to use the -by statenum:- prefix. But to use -by- prefixes, the data must be sorted on the variables you set out in the prefix (or you can use -bysort- instead of -by-, or -by varlist , sort- to tell Stata to sort the data that way now.) In my code, there are no explicit -by- statements so the issue does not arise. The use of -xtset- and the F1. operator accomplish the same effects, but are more robust in the event there are gaps in the date sequence.

    As an aside, in an earlier thread, you were asked to post example data using -dataex- so that those who want to help you can quickly and effortlessly reproduce your example data in their own Stata set up and begin to work on it. If you do not have -dataex- installed, run -ssc install dataex-, and then read -help dataex- to learn the simple instructions for using it. Please post all future data examples using -dataex-. Don't put obstacles in the path of those who want to help you.

    Comment


    • #3
      Thanks very much! That makes sense, I've now separated the data to get the growth and merged back.
      I've also installed dataex for the future.

      Comment

      Working...
      X