如何在 Stata 中将变量制表以显示样本中的所有值,即使它们尚未在数据集中?

How do I tabulate a variable in Stata to show all values that are in my sample, even if they're not yet in the dataset?

我正在尝试将工作报告的变量制成表格,以显示市政当局当前正在进行的一项调查的完成率。每周都会下载一个数据集,显示到目前为止的所有回复,我需要生成一个 table 来显示样本中所有城市的频率,即使它们为零。使用缺少的选项不起作用,因为生成报告的数据集无法告诉 stata 市政当局的总体样本是什么。

有没有办法让它根据整个样本中的值列表,将不在数据集中的值显示为零?我精通 R,所以如果有人对如何在 R 中执行此操作有建议,那也很好。

到目前为止这是我的代码:

将字符串变量转换为带有值标签的数值变量

    encode municipality_name, gen(municipality)

将市政当局完成的调查导出到 .doc 文件

    asdoc tab municipality, mis

来自 SSC 的社区贡献的命令 fre 允许将未出现在数据集中的标记值列为具有零频率的表格。这是一个例子:

. sysuse auto, clear
(1978 Automobile Data)

. fre foreign

foreign -- Car type
----------------------------------------------------------------
                   |      Freq.    Percent      Valid       Cum.
-------------------+--------------------------------------------
Valid   0 Domestic |         52      70.27      70.27      70.27
        1 Foreign  |         22      29.73      29.73     100.00
        Total      |         74     100.00     100.00           
----------------------------------------------------------------

. label def origin 42 "Extraterrestrial", add

. fre foreign

foreign -- Car type
----------------------------------------------------------------
                   |      Freq.    Percent      Valid       Cum.
-------------------+--------------------------------------------
Valid   0 Domestic |         52      70.27      70.27      70.27
        1 Foreign  |         22      29.73      29.73     100.00
        Total      |         74     100.00     100.00           
----------------------------------------------------------------

. fre foreign, includelabeled

foreign -- Car type
-------------------------------------------------------------------------
                            |      Freq.    Percent      Valid       Cum.
----------------------------+--------------------------------------------
Valid   0  Domestic         |         52      70.27      70.27      70.27
        1  Foreign          |         22      29.73      29.73     100.00
        42 Extraterrestrial |          0       0.00       0.00     100.00
        Total               |         74     100.00     100.00           
-------------------------------------------------------------------------

. ssc desc fre

--------------------------------------------------------------------------------------------------------
package fre from http://fmwww.bc.edu/repec/bocode/f
--------------------------------------------------------------------------------------------------------

TITLE
      'FRE': module to display one-way frequency table

DESCRIPTION/AUTHOR(S)

        fre displays, for each specified variable, a univariate
      frequency table containing counts, percent, and cumulative
      percent. Variables may be string or numeric. Labels, in full
      length, and values are printed. By default, fre only tabulates
      the smallest and largest 10 values (along with all missing
      values), but this can be changed. Furthermore,  values with zero
      observed frequency may be included in the  tables. The default
      for fre is to display the frequency  tables in the results
      window. Alternatively, the tables may be written to a file on
      disk, either tab-delimited or  LaTeX-formatted.

      KW: data management
      KW: frequencies
      KW: frequency table
      KW: tabulation

      Requires: Stata version 9.2

      Distribution-Date: 20150603

      Author: Ben Jann, University of Bern
      Support: email jann@soz.unibe.ch


INSTALLATION FILES                                  (type net install fre)
      fre.ado
      fre.hlp

ANCILLARY FILES                                     (type net get fre)
      fre.zip
--------------------------------------------------------------------------------------------------------
(type ssc install fre to install)