美文网首页
by sort— Repeat Stata command on

by sort— Repeat Stata command on

作者: 松柏林stata | 来源:发表于2019-04-27 07:43 被阅读0次

Description

大多数Stata命令允许使用 by 前缀,该前缀为 varlist 中变量的值相同的每组观察重复命令。 没有 sort 选项要求数据按 varlist 排序; 见[D]sort。 使用 by 前缀的 Stata 命令通过报告紧接着它们的语法图表示,例如,by is allowed ; 参见[D] by“bootstrap,by 等; 见 [U] 11.1.10 Prefix commands“。bybysort 实际上是相同的命令; bysort 只是通过sort选项
varlist1(varlist2) 语法对程序员特别有用。 它验证数据是否按 varlist1 varlist2 排序,然后执行,就好像只指定了 varlist1 一样。例如,by pid (time): generate growth = (bp - bp[ n-1])/bp,通过 pid 的值执行生成,但首先验证数据是否按照 pid 中的 pidtime 排序。

Quick start

catvar 的每个级别内生成 newv 作为观察数

by catvar: generate newv = _n

同上所述,但首先按 catvar 排序数据。

by catvar, sort: generate newv = _n

同上所述

同上所述,但在 catvar 的值内按 v 排序

bysort catvar (v): generate newv = _n

生成newv作为catvar和v水平的每个样本的观察数
Generate newv as an observation number for each observation in levels of catvar and v

bysort catvar v: generate newv = _n

注意:任何接受by前缀的命令都可以代替上面的generate

Syntax

by varlist: stata_cmd
bysort varlist: stata_cmd

上面的图表显示了by和bysort,因为它们经常被使用。 命令的完整语法是

by varlist1 [(varlist2)] [, sort rc0]:  stata_cmd
 bysort varlist1 [(varlist2)] [, rc0]:  stata_cmd

Options

sort: 指定如果数据尚未按varlist排序,则应对它们进行排序。 specifies that if the data are not already sorted by varlist, by should sort them.
rc0: 指定即使 stata cmd 在其中一个 by-groups 中产生错误,那么仍然要在剩余的 by-groups 上运行 stata cmd 。 默认操作是在发生错误时停止。 当 stata cmd 是估计命令并且一些副组没有足够的观察值时,rc0特别有用。specifies that even if the stata cmd produces an error in one of the by-groups, then by is still to run the stata cmd on the remaining by-groups. The default action is to stop when an error occurs. rc0 is especially useful when stata cmd is an estimation command and some by-groups have insufficient observations.

Example 1

use http://www.stata-press.com/data/r15/autornd
keep in 1/20
 by mpg: egen mean_w = mean(weight)

not sorted
r(5);

sort mpg
by mpg: egen mean_w = mean(weight)
list

by 要求对数据进行排序。 在上面的例子中,我们可以输入 by mpg, sort: egen mean w = mean(weight)bysort mpg:egen mean w = mean(weight) 而不是单独排序; 更多的例子,请参见** [U] 11.1.2 by varlist:, [U] 11.5 by varlist: construct,, and [U] 27.2 The by construct。 有关详细示例的扩展介绍,请参阅 Cox (2002)Mitchell (2010,chap. 7)

Technical note

通过重复 ** varlist** 定义的每个组的 stata cmd 。 如果 ** stata cmd** 存储结果,则仅存储 ** stata cmd** 执行的最后一个组的结果。

相关文章

网友评论

      本文标题:by sort— Repeat Stata command on

      本文链接:https://www.haomeiwen.com/subject/qfxhnqtx.html