Hello all,
I am trying to first generate summary statistics of some variables (by percentile) for each year and then export the output table into excel file.
(Just for the quick reference, this post is an extension to Generate table to describe distribution of a variable that is below certain percentile by year - Statalist).
I am just making up the sample of my dataset (firm-level panel data) which looks like:
I have tried the below code:
which is not working well..
Could someone help me solving this problem please?
Thanks a lot in advance!
I am trying to first generate summary statistics of some variables (by percentile) for each year and then export the output table into excel file.
(Just for the quick reference, this post is an extension to Generate table to describe distribution of a variable that is below certain percentile by year - Statalist).
I am just making up the sample of my dataset (firm-level panel data) which looks like:
ID | Year | VA | GO | Wage | Labour cost | L | capital |
1 | 2000 | 98 | 999 | 829 | 9280 | 09 | 09 |
1 | 2001 | 279 | 897 | 097 | 7809 | 89 | 79 |
1 | 2002 | 098 | 087 | 76 | 8768 | 87 | 76 |
1 | 2003 | 87 | 76 | 756 | 9887 | 987 | 987 |
1 | 2004 | 987 | 976 | 7687 | 76 | 4 | 456 |
2 | 2019 | 675 | 765 | 675 | 57 | 465 | 654 |
2 | 2020 | 654 | 876 | 876876 | 654 | 654 | 65 |
3 | 2013 | 876 | 876 | 876 | 876 | 876 | 876 |
3 | 2014 | 987 | 987 | 987 | 987 | 987 | 987 |
Code:
local varlist var1 var2 var3 var4 var5 ... (so on) collect clear levelsof $yr, local(years) foreach y of local years { foreach var of local varlist { summ `varlist' if $yr == `y', detail dtable if year == `y', /// sample( , stat(freq)) /// cont(${vars_to_check}, stat(mean p1 p5 p10 p25 p50 p75 p90 p95 p99)) /// name(yr_`y') collect addtag Years[`y'] local collections `collections' yr_`y' } } collect combine comb = `collections' collect label levels result frequency "N", modify foreach n of numlist 1 5 10 25 50 75 90 95 99 { collect remap result = percentile, fortags(result[p`n']) collect label levels percentile p`n' "`n'%", modify } collect label dim percentile "Percentiles", modify collect style header percentile, title(label) collect style header result, title(hide) level(label) collect style cell result[frequency] percentile, nformat(%3.0f) collect style cell result[mean], nformat(%5.2f) collect style cell percentile#cell_type[column-header], border(bottom, color(black)) collect layout (Years) (result[frequency mean] percentile[p1 p5 p10 p25 p50 p75 p90 p95 p99]) collect title "Distribution of variables of interest in each year" collect export "sum_stat.xlsx" sheet("`var'") sheetreplace, replace
Could someone help me solving this problem please?
Thanks a lot in advance!
Comment