【问题标题】:SPSS group by rows and concatenate string into one variableSPSS按行分组并将字符串连接成一个变量
【发布时间】:2021-07-28 15:32:35
【问题描述】:

我正在尝试使用 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 诡计,但有什么想法可以做到这一点吗?

【问题讨论】:

  • 嘿,“挑战 SPSS 的极限”??多一点功劳;)

标签: aggregate string-concatenation spss


【解决方案1】:

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

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

【讨论】:

  • 好的,所以它是使用 SORTLAG$casenum 的组合我对 SPSS 没有太多经验,所以这有很大帮助。 :) 这是完成将 SPSS 元数据导出到 REDCap 的脚本的最后一块拼图,非常感谢
  • 乐于助人!请注意,还有其他可能的解决方案 - 包括使用 casestovars 进行重组,然后将创建的新变量附加在一起 ​​- 但我认为我上面的解决方案在这种情况下更容易实现自动化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多