Stata:第一个事件,具有by变量的唯一事件的总和

Stata:第一个事件,具有by变量的唯一事件的总和,stata,Stata,以下示例数据包含描述多个玩家下注的变量 我如何计算每个球员的第一次下注类型、第一次下注价格、足球下注数量、棒球下注数量、每个客户的唯一价格数量以及每个用户名的唯一下注类型数量 clear input str16 username str40 betdate stake str16 bettype betprice str16 sport player1 "12NOV2008 12:04:33" 90 SGL 5 SOCCER player1 "04NOV2008:09:03:44" 30 SGL

以下示例数据包含描述多个玩家下注的变量

我如何计算每个球员的第一次下注类型、第一次下注价格、足球下注数量、棒球下注数量、每个客户的唯一价格数量以及每个用户名的唯一下注类型数量

clear
input str16 username str40 betdate stake str16 bettype betprice str16 sport
player1 "12NOV2008 12:04:33" 90 SGL 5 SOCCER
player1 "04NOV2008:09:03:44" 30 SGL 4  SOCCER
player2 "07NOV2008:14:03:33" 120 SGL 5 SOCCER
player1 "05NOV2008:09:00:00" 50 SGL 4 SOCCER
player1 "05NOV2008:09:05:00" 30 DBL 3 BASEBALL 
player1 "05NOV2008:09:00:05" 20 DBL 4 BASEBALL 
player2 "09NOV2008:10:05:10" 10 DBL 5 BASEBALL 
player2 "15NOV2008:15:05:33" 35 DBL 5 BASEBALL 
player1 "15NOV2008:15:05:33" 35 TBL 5 BASEBALL
player1 "15NOV2008:15:05:33" 35 SGL 4 BASEBALL
end

generate double timestamp=clock(betdate,"DMY hms") 
format timestamp %tc

generate double dateonly=date(betdate,"DMY hms") 
format dateonly %td

generate firsttype
generate firstprice
generate soccercount
generate baseballcount
generate uniquebettypecount
generate uniquebetpricecount

这是一个“请给我代码”的问题,有点接近边际,没有尝试自己的解决方案

第一种类型和价格是

bysort username (timestamp) : gen firsttype = bettype[1] 
bysort username (timestamp) : gen firstprice = betprice[1] 
足球和棒球投注的数量为

egen soccercount = total(sport == "SOCCER"), by(username) 
egen baseballcount = total(sport == "BASEBALL"), by(username) 
不同[非唯一!]下注类型的数量为

bysort username bettype : gen work = _n == 1 
egen uniquebettypecount = total(work), by(username) 
另一个问题也一样(但是
替换工作
)。另一种方法是

egen work = tag(username bettype) 
egen uniquebettypecount = total(work), by(username) 
所有这些变量的特点是,对每组中的所有值重复相同的值。例如,
firsttype
对于每个不同的
用户名的每次出现都具有相同的值。通常,您只需要使用每个值一次。例如,刚刚使用的
egen
函数
tag()

egen usertag = tag(username) 
在需要时使用
if usertag
。(
if-usertag
对于
if-usertag==1
是一个有用的习惯用法)

一些阅读建议:

by:

egen上


根据不同的观察结果(以及为什么“独特”一词具有误导性):

这是一个“请给我代码”的问题,没有尝试自己的解决方案

第一种类型和价格是

bysort username (timestamp) : gen firsttype = bettype[1] 
bysort username (timestamp) : gen firstprice = betprice[1] 
足球和棒球投注的数量为

egen soccercount = total(sport == "SOCCER"), by(username) 
egen baseballcount = total(sport == "BASEBALL"), by(username) 
不同[非唯一!]下注类型的数量为

bysort username bettype : gen work = _n == 1 
egen uniquebettypecount = total(work), by(username) 
另一个问题也一样(但是
替换工作
)。另一种方法是

egen work = tag(username bettype) 
egen uniquebettypecount = total(work), by(username) 
所有这些变量的特点是,对每组中的所有值重复相同的值。例如,
firsttype
对于每个不同的
用户名的每次出现都具有相同的值。通常,您只需要使用每个值一次。例如,刚刚使用的
egen
函数
tag()

egen usertag = tag(username) 
在需要时使用
if usertag
。(
if-usertag
对于
if-usertag==1
是一个有用的习惯用法)

一些阅读建议:

by:

egen上


根据不同的观察结果(以及为什么“独特”一词具有误导性):

这是一个“请给我代码”的问题,没有尝试自己的解决方案

第一种类型和价格是

bysort username (timestamp) : gen firsttype = bettype[1] 
bysort username (timestamp) : gen firstprice = betprice[1] 
足球和棒球投注的数量为

egen soccercount = total(sport == "SOCCER"), by(username) 
egen baseballcount = total(sport == "BASEBALL"), by(username) 
不同[非唯一!]下注类型的数量为

bysort username bettype : gen work = _n == 1 
egen uniquebettypecount = total(work), by(username) 
另一个问题也一样(但是
替换工作
)。另一种方法是

egen work = tag(username bettype) 
egen uniquebettypecount = total(work), by(username) 
所有这些变量的特点是,对每组中的所有值重复相同的值。例如,
firsttype
对于每个不同的
用户名的每次出现都具有相同的值。通常,您只需要使用每个值一次。例如,刚刚使用的
egen
函数
tag()

egen usertag = tag(username) 
在需要时使用
if usertag
。(
if-usertag
对于
if-usertag==1
是一个有用的习惯用法)

一些阅读建议:

by:

egen上


根据不同的观察结果(以及为什么“独特”一词具有误导性):

这是一个“请给我代码”的问题,没有尝试自己的解决方案

第一种类型和价格是

bysort username (timestamp) : gen firsttype = bettype[1] 
bysort username (timestamp) : gen firstprice = betprice[1] 
足球和棒球投注的数量为

egen soccercount = total(sport == "SOCCER"), by(username) 
egen baseballcount = total(sport == "BASEBALL"), by(username) 
不同[非唯一!]下注类型的数量为

bysort username bettype : gen work = _n == 1 
egen uniquebettypecount = total(work), by(username) 
另一个问题也一样(但是
替换工作
)。另一种方法是

egen work = tag(username bettype) 
egen uniquebettypecount = total(work), by(username) 
所有这些变量的特点是,对每组中的所有值重复相同的值。例如,
firsttype
对于每个不同的
用户名的每次出现都具有相同的值。通常,您只需要使用每个值一次。例如,刚刚使用的
egen
函数
tag()

egen usertag = tag(username) 
在需要时使用
if usertag
。(
if-usertag
对于
if-usertag==1
是一个有用的习惯用法)

一些阅读建议:

by:

egen上

根据不同的观察结果(以及为什么“独特”一词具有误导性):