0

我正在尝试为多成员多级模型准备我的数据集,并且在如何创建一系列“多成员标识符”变量上陷入困境。本质上,我有带有个人标识符 (ID) 的数据以及他们从 1996 年到 1999 年(state_1996 到 state_1999)所居住的州,其中 1=阿拉斯加,2=亚利桑那等。例如(这是编造的):

ID state_1996 state_1997 state_1998 state_1999

1 1 1 2 2

2 1 1 1 1

3 3 1 1 1

n 4 4 4 4

我正在尝试创建变量 s1 到 s51 来给出每个人在每个州花费的时间比例。例如,根据上表并仅给出这些新变量的一个子集,我想要如下所示的内容:

标识 s1 s2 s3 s4

1 0.5 0.5 0 0

2 1.0 0 0 0

3 0.75 0 0.25 0

n 0 0 0 1.0

任何有关最佳方式的帮助将不胜感激,谢谢!

4

1 回答 1

0

这是一个非常简单的版本,应该可以满足您的需求:

forv i = 1/51{
    gen s`i' = 0
    forv year = 1996/1999{
        replace s`i' = s`i' + 0.25 if state_`year' == `i'
    }
}

这是一个更通用的版本,应该适用于任意数量的州和年份,以防它对任何人有用:

// Count how many years there are
ds state_????
local yearVars `r(varlist)'
local numYears: list sizeof yearVars
di `numYears'

// What fraction of the total time is one year?
local myFrac = 1/`numYears'
di `myFrac'

// Get a list of all values state_???? can take
foreach var in `yearVars'{
    levelsof `var'
    local tempYears `r(levels)'
    local allStates: list allStates | tempYears
}

// Loop over all states to create the variables you want
foreach i in `allStates'{
    gen s`i' = 0
    foreach var in `yearVars'{
        replace s`i' = s`i' + `myFrac' if `var' == `i'
    }
    // Make sure you don't get any wacky values here
    assert s`i' >=0 & s`i' <= 1
}

// Make sure total time adds up to 1
egen totaltime = rowtotal(s? /* s?? */) // uncomment s?? if you've got between 10 and 99 states; I bet you do
assert totaltime == 1
drop totaltime
于 2020-08-19T03:25:06.610 回答