SPSS按行分组并将字符串连接成一个变量 [英] SPSS group by rows and concatenate string into one variable

查看:62
本文介绍了SPSS按行分组并将字符串连接成一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 使用 SPSS 语法将 SPSS 元数据 导出为自定义格式.带有值标签的数据集包含一个或多个变量标签.

I'm trying to export SPSS metadata to a custom format using SPSS syntax. The dataset with value labels contains one or more labels for the variables.

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

However, now I want to concatenate the value labels into one string per variable. For example for the variable SEX combine or group the rows F/Female and M/Male into one variable F=Female;M=Male;. I already concatenated the code and labels into a new variable using Compute CodeValueLabel = concat(Code,'=',ValueLabel). so the starting point for the source dataset is like this:

+--------------+------+----------------+------------------+
| 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 |
+--------------+------+----------------+------------------+

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

The goal is to get a dataset something like this:

+--------------+-------------------------------------------------+
| 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 技巧,但对如何做到这一点有任何想法吗?

I tried using CASESTOVARS but that creates separate variables, so several variables not just one single string variable. I'm starting to suspect that I'm running up against the limits of what SPSS can do. Although maybe it's possible using some AGGREGATE or OMS trickery, any ideas on how to do this?

推荐答案

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

First I recreate your example here to demonstrate on:

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 足够宽以包含填充最多的变量的所有值.

NOTE: make combineall wide enough to contain all the values of your most populated variable.

这篇关于SPSS按行分组并将字符串连接成一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆