How to make Stata report zeros in a table

I am trying to use a command tabulatein Stata to create a time series of frequencies. The problem occurs when I try to combine the output tabulateafter each date is completed. tabulatewill not contain 0 as a record if there is no observation for the value of the variable. For example, if I wanted to count 10, 11, and 12-year-olds in a class over a three-year period, Stata could infer (8) if only one of the groups was represented, and therefore we do not know which group the students belonged: it could be (0.8.0) or (0.0.8).

This is not a problem if the time series is short, since the Results window shows which categories are presented or not presented. I have a lot more time series according to my data. Does anyone know of a solution / method that causes Stata to include zeros in these tables? The relevant parts of my code are listed below:

# delimit;
set more off;
clear;
matrix drop _all;
set mem 1200m;
cd ;
global InputFile "/Users/.../1973-2010.dta";
global OutputFile "/Users/.../results.txt";

use $InputFile;
compress;

log using "/Users/.../log.txt", append;

gen yr_mn = ym(year(datadate), month(datadate));
la var yr_mn "Year-Month Date"

xtset, clear;
xtset id datadate, monthly;

/*Converting the Ratings Scale to Numeric*/;
gen LT_num = .;
replace LT_num = 1 if splticrm=="AAA";
replace LT_num = 2 if (splticrm=="AA"||splticrm=="AA+"||splticrm=="AA-");
replace LT_num = 3 if (splticrm=="A"||splticrm=="A+"||splticrm=="A-");
replace LT_num = 4 if (splticrm=="BBB"||splticrm=="BBB+"||splticrm=="BBB-");
replace LT_num = 5 if (splticrm=="BB"||splticrm=="BB+"||splticrm=="BB-");
replace LT_num = 6 if (splticrm=="B"||splticrm=="B+"||splticrm=="B-");
replace LT_num = 7 if (splticrm=="CCC"||splticrm=="CCC+"||splticrm=="CCC-");
replace LT_num = 8 if (splticrm=="CC");
replace LT_num = 9 if (splticrm=="SD");
replace LT_num = 10 if (splticrm=="D");

summarize(yr_mn);
local start = r(min);
local finish = r(max);

forv x = `start'/`finish' {;
    qui tab LT_num if yr_mn == `x', matcell(freq_`x');
};

log close;
+3
source share
3 answers

What you want is not an option with a command tab. If you want to display the results on the screen, you can successfully use table ..., missing.

Instead of a loop, you can try the following, which I think will work for your purposes:

preserve
gen n = 1  // (n could be a variable that indicates if you want to include the row or not; or just something that never ==.)
collapse (count) n , by(LT_num yr_mn)
reshape wide n, i(yr_mn) j(LT_num)
mkmat _all , matrix(mymatname) 
restore
mat list mymatname

I think this is what you are going to do (but cannot tell how you use the matrices that you are trying to generate).

P.S. inlist , :

replace LT_num = 2 if inlist(splticrm,"AA","AA+","AA-")
+2
+2

, . , , , .

( ) , . , . "local finish = r (max)". [ , , , .]

local counter=0;
forv x = `first'/`last' {;
tab LT_num if yr_mn == `x', matrow(index_`x') matcell(freq_`x');
local rows = r(r); /*r(r) is number of rows for tabulate*/;

if `rows'!=0{;
    matrix define A_`x'=J(10,1,0);
    forv r=1/`rows'{;
        local a=index_`x'[`r',1];
        matrix define A_`x'[`a',1]=freq_`x'[`r',1];
    };
};
else {;
    local counter=`counter'+1;
};
};   


local start=`first'+`counter'+1;
matrix define FREQ = freq_`start';

forv i = `start'/`last' {;
    matrix FREQ = (FREQ,A_`i');
};
0

Source: https://habr.com/ru/post/1792129/


All Articles