如果没有一些编程工作,我认为你不能得到这种行为.请注意,tabulate需要varnames或varlists和表情都是不允许的.见help tabulate.当然,generate是一个允许表达式的命令.看help exp一些细节.
我记得在Statalist中有关"实时计算"的讨论,但我现在无法找到它; 你可能想检查一下档案.如果我没记错的话,除了因子变量以外你不能使用因子变量符号(help factor variables)"动态"使用.我记得有充分的理由说明为什么会这样.
但是对于类似两个变量的产品,您可以在.ado文件中编写一个简单的程序,将其保存在Stata系统目录中(最好是〜/ ado/personal /),然后随时使用它.
这是一个示例程序:
program define tabulate2, byable(recall)
/*
Tabulate the product of two variables.
Modeled after the official -tab1- command.
*/
version 12
syntax varlist(min=2 max=2 numeric) [if] [in] [, *]
tokenize `varlist'
tempvar prod
gen `prod' = `1' * `2'
label var `prod' "`1' x `2'"
tempvar touse
mark `touse' `if' `in'
capture noisily tabulate `prod' if `touse', `options'
end
Run Code Online (Sandbox Code Playgroud)
还有一些代码使用了新定义的命令tabulate2:
clear all
set more off
input ///
var1 var2
1 2
3 4
3 4
3 4
5 6
end
list
tabulate2 var1 var2
Run Code Online (Sandbox Code Playgroud)
输出:
. tabulate2 var1 var2
var1 x var2 | Freq. Percent Cum.
------------+-----------------------------------
2 | 1 20.00 20.00
12 | 3 60.00 80.00
30 | 1 20.00 100.00
------------+-----------------------------------
Total | 5 100.00
Run Code Online (Sandbox Code Playgroud)
不完美,但我希望你明白这个想法.
另见help program其中的参考文献.