我有一个如下所示的数据集:
..。
我想要做的是计算一组21个新的变量,显示每一个案例(媒体)、七个变量(分数)中的每一个、那个特定渠道的得分与三个感兴趣的渠道的分数之间的差异:卫报、电报和独立(7个变量X3基准outlets=21)。从本质上说,我想把每一家分店的得分与我的三家基准网点进行比较。
例如,我应该有一个名为score1_Guardian的新变量,对于出口1,它将被计算为:该变量的得分出口1--卫报为该变量获得的分数。变量score2_Guardian将显示,对于每个出口,每个特定出口在该变量上的得分与守护者为该变量获得的分数之间的差异,依此类推。所以在这个例子中,“卫报”的出口将在所有score1_Guardian到score7_Guardian变量上得分为0。
发布于 2019-01-08 15:23:43
有比我下面建议的更简单的方法来做到这一点,但是我更喜欢这种方式--少代码和少临时变量。
首先,我根据您的参数创建一个假数据集:
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="_".https://stackoverflow.com/questions/54092772
复制相似问题