使用 SPSS 参考变量和索引创建新变量

Using SPSS Reference Variables and an Index to Create a New Variable

基本上,我有一个日志,其中包含通过多个案例跟踪的主题的唯一标识符。然后我使用了以下代码,之前通过这里的伟大社区建议的,来创建一个索引。不幸的是,我 运行 遇到了一个我似乎无法理解的新挑战。这是当前数据集的示例,以提供透视图。

索引函数

sort cases by Unique_Modifier.
if $casenum=1 or Unique_Modifier<>lag(Unique_Modifier) Index=1.
if Unique_Modifier=lag(Unique_Modifier) Index=lag(Index)+1.
format Index(f2).
execute. 

Unique Identifier   Index   Variable of interest
A                    1          101
A                    2          101
A                    3          607
A                    4          607
A                    5          101
A                    6          101
B                    1          108
B                    2          210
C                    1          610
C                    2          987
C                    3         1100
C                    4          610

我想做的是创建一个新变量,其中包含感兴趣的变量列中离散的不同条目的数量。预期输出如下:

Unique Identifier   Index   Variable of interest    Intended Output
A                       1               101               1
A                       2               101               1
A                       3               607               2
A                       4               607               2
A                       5               101               2
A                       6               101               2
B                       1               108               1
B                       2               210               2
C                       1               610               1
C                       2               987               2
C                       3               1100              3
C                       4               610               3

我尝试了几种不同的方法,一种是使用类似的索引函数,但它失败了,好像后续行中感兴趣的变量不同,它有效,但有时,我们有一个5 行后变量的重复出现。我的下一个想法是使用 AGGREGATE 函数,但我查看了 IBM 手册,似乎聚合中没有一个函数可以在这里产生预期的输出。谁有想法?我认为循环是最好的选择,但 SPSS 中的循环有点时髦且难以工作。

试试这个:

data list list/Unique_Identifier   Index   VOI (3f)  .
begin data.
1                       1               101               
1                       2               101               
1                       3               607               
1                       4               607               
1                       5               101               
1                       6               101               
2                       1               108               
2                       2               210               
3                       1               610               
3                       2               987               
3                       3               1100             
3                       4               610               
end data.

string voiT (a1000).
compute voiT=concat(ltrim(string(VOI,f10)),",").
compute Intended_Output=1.
do if index>1.
   do if index(lag(voiT), rtrim(voiT))>0.
      compute Intended_Output=lag(Intended_Output).
      compute voiT=lag(voiT).
   else.
      compute Intended_Output=lag(Intended_Output)+1.
      compute voiT=concat(rtrim(lag(voiT)), rtrim(voiT)).
   end if.
end if .
exe.