比较不同行中的值以创建新变量
Comparing values in different rows to create new variable
我有一个如下所示的数据集:
outlet name(字符串变量):媒体名称(最多12个),文件中最后三个outlets是The Guardian,The Telegraph和The Independent。
得分 1:规模
得分 2:规模
...
- 分数 7:规模。
我想做的是计算一组 21 个新变量,这些变量显示每个案例(媒体渠道),七个变量(分数)中的每一个,该特定渠道的分数之间的差异,以及三个感兴趣的媒体的分数:卫报、电讯报和独立报(7 个变量 X 3 个基准媒体 = 21)。本质上,我想将每个网点的分数与我的三个基准网点进行比较。
因此,例如我应该有一个名为 score1_Guardian 的新变量,对于出口 1 将计算为:出口 1 为该变量获得的分数 - The Guardian 为该变量获得的分数。变量 score2_Guardian 将针对每个出口显示每个特定出口在该变量上获得的分数与卫报在该变量上获得的分数之间的差异,等等。因此,在这个例子中,出口 The Guardian 将在所有 score1_Guardian 到 score7_Guardian 变量上得分为 0。
有比我在下面建议的方法更简单的方法来执行此操作,但我更喜欢这种方法 - 更少的代码和更少的临时变量。
首先我根据你的参数创建了一个假数据集:
data list list/outlet (a12) score1 to score7 (7f6).
begin data
'outlet1' 1 2 3 4 5 6 7
'outlet2' 2 3 4 5 6 7 8
'outlet3' 5 6 7 8 9 1 2
'Guardian' 7 8 9 1 2 5 6
'Telegraph' 5 12 12 3 4 4 2
'Independent' 2 2 2 2 2 2 2
end data.
现在我们可以开始工作了:
*going from wide to long form - just to avoid creating too many variables on the way.
varstocasese /make score from score1 to score7/index scorenum(score).
if outlet='Guardian' Guardian=score.
if outlet='Telegraph' Telegraph=score.
if outlet='Independent' Independent=score.
AGGREGATE /OUTFILE=* MODE=ADDVARIABLES OVERWRITEVARS=YES
/BREAK=scorenum /Guardian=MAX(Guardian) /Telegraph=MAX(Telegraph) /Independent=MAX(Independent).
*now we have three new variables ready to compare.
compute Guardian=score - Guardian.
compute Telegraph=score - Telegraph.
compute Independent=score - Independent.
* last step - going back to wide format.
compute scorenum=substr(scorenum,6,1).
CASESTOVARS /id=outlet /index=scorenum/sep="_".
我有一个如下所示的数据集:
outlet name(字符串变量):媒体名称(最多12个),文件中最后三个outlets是The Guardian,The Telegraph和The Independent。
得分 1:规模
得分 2:规模
...
- 分数 7:规模。
我想做的是计算一组 21 个新变量,这些变量显示每个案例(媒体渠道),七个变量(分数)中的每一个,该特定渠道的分数之间的差异,以及三个感兴趣的媒体的分数:卫报、电讯报和独立报(7 个变量 X 3 个基准媒体 = 21)。本质上,我想将每个网点的分数与我的三个基准网点进行比较。
因此,例如我应该有一个名为 score1_Guardian 的新变量,对于出口 1 将计算为:出口 1 为该变量获得的分数 - The Guardian 为该变量获得的分数。变量 score2_Guardian 将针对每个出口显示每个特定出口在该变量上获得的分数与卫报在该变量上获得的分数之间的差异,等等。因此,在这个例子中,出口 The Guardian 将在所有 score1_Guardian 到 score7_Guardian 变量上得分为 0。
有比我在下面建议的方法更简单的方法来执行此操作,但我更喜欢这种方法 - 更少的代码和更少的临时变量。
首先我根据你的参数创建了一个假数据集:
data list list/outlet (a12) score1 to score7 (7f6).
begin data
'outlet1' 1 2 3 4 5 6 7
'outlet2' 2 3 4 5 6 7 8
'outlet3' 5 6 7 8 9 1 2
'Guardian' 7 8 9 1 2 5 6
'Telegraph' 5 12 12 3 4 4 2
'Independent' 2 2 2 2 2 2 2
end data.
现在我们可以开始工作了:
*going from wide to long form - just to avoid creating too many variables on the way.
varstocasese /make score from score1 to score7/index scorenum(score).
if outlet='Guardian' Guardian=score.
if outlet='Telegraph' Telegraph=score.
if outlet='Independent' Independent=score.
AGGREGATE /OUTFILE=* MODE=ADDVARIABLES OVERWRITEVARS=YES
/BREAK=scorenum /Guardian=MAX(Guardian) /Telegraph=MAX(Telegraph) /Independent=MAX(Independent).
*now we have three new variables ready to compare.
compute Guardian=score - Guardian.
compute Telegraph=score - Telegraph.
compute Independent=score - Independent.
* last step - going back to wide format.
compute scorenum=substr(scorenum,6,1).
CASESTOVARS /id=outlet /index=scorenum/sep="_".