Announcement

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

  • How to generate normally distributed time-invariant covariates in panel data?

    I am trying to generate panel data.
    So, I first made the panel and time variables as below.
    Code:
    local N  = 1000 // # units
    local T  = 3 // # periods
    local NT = `N' * `T'
    
    set obs `NT'
    egen pid = seq(), block(`T')
    egen tid = seq(), to(`T')
    Here, I want to generate normally distributed time-invariant covariates.
    Specifically, what I want to generate is
    Code:
    mata:
    N = 1000
    T = 3
    rnormal(N, 2, 0, 1) # J(T, 1, 1)
    end
    However, I am not sure how to do that in Stata.

    (I am trying to do not use loop or reshape or Mata)

  • #2
    Code:
    clear*
    
    local N  = 1000 // # units
    local T  = 3 // # periods
    local NT = `N' * `T'
    
    set obs `NT'
    egen pid = seq(), block(`T')
    egen tid = seq(), to(`T')
    
    set seed 1234 // OR WHATEVER YOU LIKE
    by pid (tid), sort: gen x = rnormal() if _n == 1
    by pid (tid): replace x = x[_n-1] if _n > 1

    Comment


    • #3
      Clyde Schechter It works perfectly! Thank you.

      Comment

      Working...
      X