SPSS按行分组并将字符串连接成一个变量

问题描述

我正在尝试使用 SPSS 语法将 export SPSS metadata 转换为自定义格式。带有值标签的数据集包含一个或多个变量标签

但是,现在我想将每个变量的值标签连接成一个字符串。例如,对于变量 SEX,将行 F/FemaleM/Male 组合或分组为一个变量 F=Female;M=Male;。我已经使用 Compute CodeValueLabel = concat(Code,'=',ValueLabel).代码标签连接到一个新变量中 所以源数据集的起点是这样的:

+--------------+------+----------------+------------------+
| VarName      | Code | ValueLabel     | CodeValueLabel   |
+--------------+------+----------------+------------------+
| SEX          | F    | Female         | F=Female         |
| SEX          | M    | Male           | M=Male           |
| ICFORM       | 1    | Yes            | 1=Yes            |
| LIMIT_DETECT | 0    | Too low        | 0=Too low        |
| LIMIT_DETECT | 1    | normal         | 1=normal         |
| LIMIT_DETECT | 2    | Too high       | 2=Too high       |
| LIMIT_DETECT | 9    | Not applicable | 9=Not applicable |
+--------------+------+----------------+------------------+

目标是获得这样的数据集:

+--------------+-------------------------------------------------+
| VarName      | group_and_concatenate                           |
+--------------+-------------------------------------------------+
| SEX          | F=Female;M=Male;                                |
| ICFORM       | 1=Yes;                                          |
| LIMIT_DETECT | 0=Too low;1=normal;2=Too high;9=Not applicable; |
+--------------+-------------------------------------------------+

我尝试使用 CASESTOVARS 但这会创建单独的变量,因此多个变量而不仅仅是一个字符串变量。我开始怀疑我是否遇到了 SPSS 所能做的极限。虽然也许可以使用一些 AGGREGATEOMS 技巧,但您有关于如何做到这一点的想法吗?

解决方法

首先,我在这里重新创建您的示例以进行演示:

data list list/varName CodeValueLabel (2a30).
begin data
"SEX"  "F=Female"
"SEX"  "M=Male"
"ICFORM"  "1=Yes"
"LIMIT_DETECT"  "0=Too low"
"LIMIT_DETECT"  "1=Normal"
"LIMIT_DETECT"  "2=Too high"
"LIMIT_DETECT"  "9=Not applicable"
end data.

现在开始工作:

* sorting to make sure all labels are bunched together.
sort cases by varName CodeValueLabel.
string combineall (a300).
* adding ";" .
compute combineall=concat(rtrim(CodeValueLabel),";").
* if this is the same varname as last row,attach the two together.
if $casenum>1 and varName=lag(varName)  
     combineall=concat(rtrim(lag(combineall))," ",rtrim(combineall)).
exe.
*now to select only relevant lines - first I identify them.
match files /file=* /last=selectthis /by varName.
*now we can delete the rest.
select if selectthis=1.
exe.

注意:使 combineall 足够宽以包含填充最多的变量的所有值。