1
2
3
4
5
6
7
8
9
10
11
12
13
** 根据X1分类,生成每组样本数
by X1: gen X2=_N //包含缺失值
by X1: gen X2=_N if X1!=. //排除缺失值

** count函数: 表达式exp计算结果不是缺失值的样本数
egen newvar = count(exp), by(varlist)

** total 函数: 表达式exp计算结果的样本数
gen count=1
egen count_num = total(exp), by(varlist)

** seq 函数: 循环计数,生成数列 (每个数字计数两次,范围3~4,重复计数: 3,3,4,4, | 3,3,4,4, | 3,……)
egen list=seq(), from(3) to(4) block(2) by(varlist)