I am trying to create a table showing the number of neighborhoods and metropolitan areas for each year of my (unbalanced) panel data set. I would like something like this :
Also, I would like to display the number of coastal neighborhoods (I have a dummy indicating the coastal status of the neighborhood) on another columns.
I have tried this code,
tabout year if tr_pop>0 & pri!=. using "$results/table1/table1.tex", stats(count summ i_shore) style(tex) replace f(0c ) clab(Neighborhoods)
but I only got one columns about the number of neighborhoods.
* Example generated by -dataex-. For more info, type help dataex
clear
input float(year metro nhood)
2010 1 1
2010 1 2
2010 2 3
2020 1 1
2020 1 2
2020 2 3
2020 3 4
end
egen tagm = tag(year metro)
egen metros = total(tagm), by(year)
egen tagn = tag(year nhood)
egen nhoods = total(tagn), by(year)
tabdisp year, c(metros nhoods)
----------------------------------
year | metros nhoods
----------+-----------------------
2010 | 2 3
2020 | 3 4
----------------------------------
.
With a coastal
(0, 1) indicator (dummy in vulgar parlance) you might just want an extra variable defined by total(coastal * tagn)
.
If each neighbourhood occurs just once in the data for each year (we can't see your data) you can go direct to
bysort year : gen nhoods = _N