Announcement

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

  • Creating a column out of the results from a loop

    I have a panel data and I have used a loop to tabulate the standard errors by stacking 3 columns together. The standard error value is estimated for each year (1993-2018).

    ​​​​​​
    forvalues i = 1993(1)2018{
    preserve
    stack column1 column2 column3 if year == `i', into(a) clear
    tabstat a, statistics( semean ) columns(variables)
    restore
    }

    The problem is that after estimation, I have to manually input each value into one new column (from 1993-2018). Is there a way to add a command in the loop so that each se value that it estimates is automatically put into the same column?

    Thank you.

  • #2
    This may be easier achieved using a reshape rather than a loop:

    Code:
    reshape long column, i(year) j(new)
    egen se = sd(column), by(year)
    bysort year (new): replace se = se / sqrt(new[_N])
    reshape wide
    If that doesn't work with your data (which in future posts is best represented by a data example using the dataex command, as explained in the FAQ), try:

    Code:
    gen se=.
    forvalues i = 1993(1)2018{
        preserve
        stack column1 column2 column3 if year == `i', into(a) clear
        tabstat a, statistics( semean ) columns(variables) save
        local a`i' = r(StatTotal)[1,1]
        restore
        replace se = `a`i'' if year==`i'
    }

    Comment

    Working...
    X