Announcement

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

  • Counting values and creating balance table with Nvals, _n, _N

    First, could someone explain in an easy way how nvals, _n and _N is used? I would like to count all distinct values of a variable and I have seen some codes online using
    Code:
    by ...: gen nvals=_n=1
    However, I have problems to understand what is done here.

    Second, I have the following code for creating a balance table:
    Code:
    local fixed v1 v2 v3 v4 v5
    
    local fixed_n: word count `fixed'
    
    forvalues i = 1 / `fixed_n' {
    local fixed_name: word `i' of `fixed'
    local fixed`i' : var label `fixed_name'}
    
    foreach v of local fixed {    
    sum `v' if subset==1 }
    
    *comparison 1
    foreach v of local fixed {    
    xtreg `v' event_1 if subset==1, fe  vce (cluster xfe) 
    outreg2 using Table1A}
    
    *comparison 2
    foreach v of local fixed {    
    xtreg `v' event_2 if subset==1, fe  vce (cluster xfe) 
    outreg2 using Table1B}
    Can someone explain what is done above to create the table? I am new to Stata and get confused by these loops and _n, so it would be great if someone could explain it step by step. Also, is there an easier way to get the same table, but with a simpler code? Thanks.

  • #2
    I will leave your question on balance tables to others. outreg2 is from SSC and a popular download but I don't ever use it.

    distinct from the Stata Journal does things like this.


    Code:
    . webuse grunfeld, clear
    
    . distinct
    
    --------------------------------
             |     total   distinct
    ---------+----------------------
     company |       200         10
        year |       200         20
      invest |       200        195
      mvalue |       200        198
      kstock |       200        199
        time |       200         20
    --------------------------------
    
    . distinct , sort
    
    --------------------------------
             |     total   distinct
    ---------+----------------------
     company |       200         10
        time |       200         20
        year |       200         20
      invest |       200        195
      mvalue |       200        198
      kstock |       200        199
    --------------------------------
    
    . distinct, sort max(10)
    
    --------------------------------
             |     total   distinct
    ---------+----------------------
     company |       200         10
    --------------------------------
    .


    Comment

    Working...
    X