我正在尝试更有效地使用分类变量。
假设我有一个分类变量phone,它具有以下值:
----------------------
phone | Freq.
----------+-----------
Landline | 223
Mobile | 49,297
Both | 1,308
Run Code Online (Sandbox Code Playgroud)
我想运行这样的命令:
sum x if phone == Mobile
Run Code Online (Sandbox Code Playgroud)
为此,我需要运行以下三个命令:
// figure out what the label is called
. describe phone, full
storage display value
variable name type format label
-------------------------------------------
phone byte %15.0g phone_label
// list the label so i can figure out what number goes with what category
. label list phone_label
phonel:
1 Landline
2 Mobile
3 Both
// run the command with the numeric category identifier
. sum x if phone == 2
Run Code Online (Sandbox Code Playgroud)
现在我的代码有一条晦涩的行phone == 2,除非他们执行上述相同的步骤,否则其他用户不会清楚。
有没有办法直接使用分类标识符“Mobile”而不是数字标识符 2?
您可以使用值标签选择观测值。
. sysuse auto, clear
(1978 Automobile Data)
. count if foreign=="Foreign":origin
22
Run Code Online (Sandbox Code Playgroud)
您需要在此处知道值标签的名称origin。您可以通过多种方式查找。
这在 Stata 14 中的 [U] 13.11 和早期版本中(可能在不同的章节和章节编号下)有记录。另见http://www.stata-journal.com/article.html?article=dm0009