How to get Stata to report zeroes in tabulate

巧了我就是萌 提交于 2019-12-06 04:11:35

What you want is not an option with the tab command. If you want to display the results to the screen, you might be able to use table ..., missing successfully.

Instead of the loop, you could 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 that is what you're going after (but can't tell how you use the matrices you are trying to generate).

P.S. I prefer to use the inlist function for things like:

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

This problem is addressed by tabcount. See the 2003 paper

http://www.stata-journal.com/article.html?article=pr0011

and download the program code and help files after getting a link by search tabcount.

This is the solution that I used. Keith's is probably better, and I will explore his solution in the future.

I saved the row labels (using matrow) in a vector and used it as an index for a matrix of the correct dimensions initialized to zero. That way I could place each frequency into the matrix at the correct place, and keep all of the zeros. The solution follows the above code after "local finish=r(max)". [note that I include a counter to eliminate the first observations which are empty for this variable.]

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');
};
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!