Hello,
My goal is to collect counts and percentage of each level of a categorical variable in a PostFile, looping several categorical variables each with a variable number of categories/levels
I have come up with the following code:
#delimit ;
local CategVar
"
sex vit_intermacs vit_nyha hx_crt hx_icd mr_degree tr_degree vad_acei vad_aldant vad_arb vad_bb vad_ecmo vad_furo vad_statin vad_strategy death
";
postfile stats str32 varname obs freq perc using TBL_RiVmiRNA_CATEG2, replace;
* LOOP FOR CATEGORICAL VARIABLES ;
foreach var of varlist `CategVar' {;
capture noisily preserve; // capture here is mandatory, the script stops otherwise, BUT we need the preserve
quietly tabulate `var', gen(`var');
drop if missing(`var');
capture noisily contract `var', percent(percentage);
egen N = total(`var');
post stats ("`var'") (N) (_freq) (percentage);
restore;
};
postutil clear;
* terminato il loop, eliminiamo dalla memoria del comando Post le statistiche or ora calcolate;
#delimit cr
That is, I guess, working fine, EXCEPT for the fact that stores in just one line only stats about the last level of the variable, there is a way to modify it so to store on several rows stats for each level of each variable ?
Thank you in advance,
Diego
Related Posts with Collecting Descriptive Stats (counts percentages) for categorical variables in a Postfile with a Loop
Commenting does not work; "/ is not a valid command name" errorHi! For some reason commenting in Stata is not working for me. If I type "// test" (in newly-opened …
Creating variable based on conditions of two variables using matrixDear everyone I'm trying to create a variable based on matrix conditions of two other variable. I …
Propensity Score Matching to estimate racial discriminationHello everyone, I am doing research on racial discrimination at the loan approval decisions, i.e. w…
The ouput of logistic regressions in a table (using outreg2, estout, asdoc, ...)Dear All, After running logistic regressions, how can I export the results, in terms of (1) coeffici…
Assigning one of two fixed values to each observation according to categories alongside continuous variablesI have a 16,000 data set with continuous dependant variables and an independent variable. I also hav…
Subscribe to:
Post Comments (Atom)
0 Response to Collecting Descriptive Stats (counts percentages) for categorical variables in a Postfile with a Loop
Post a Comment