SPSS Loop:解析 n 个变量以获得值列表
SPSS Loop: parse n variables for list of values
我想使用针列表遍历多个变量(即多个干草堆)。
数据:
PICK1
PICK2
PICK3
PICK4
Grape
Raspberry
Clementime
Strawberry
Strawberry
Lemon
Blueberry
Apple
Cantelope
Mango
Grape
Pear
Apple
Orange
Kiwi
Raspberry
有效的手动代码
Compute citrus=0
if char.index(PICK1,"orange")>0 citrus=1.
if char.index(PICK2,"orange")>0 citrus=1.
if char.index(PICK3,"orange")>0 citrus=1.
if char.index(PICK4,"orange")>0 citrus=1.
if char.index(PICK1,"lemon")>0 citrus=1.
if char.index(PICK2,"lemon")>0 citrus=1.
if char.index(PICK3,"lemon")>0 citrus=1.
if char.index(PICK4,"lemon")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
期望的结果:如果在变量中找到值,则记录 1
Citrus
1
1
0
1
尝试的代码(不工作)
do repeat L = [orange, lemon, clementine]
/PICK = PICK1 to PICK7000.
compute L = if char.index(PICK,L)>0 citrus=1.
end repeat.
我最初使用 char.index 来搜索具有相似基本名称的多个值。例如,搜索“berry”会希望找到“strawberry”、“blueberry”等。有没有办法将其合并到 COMPUTE
函数中?
NUMERIC berry (f1.0).
DO REPEAT PICK = PICK1 to PICK7000.
COMPUTE berry=any(PICK, char.index(PICK, "berry").
END REPEAT.
EXECUTE.
do repeat
结构使用长度必须相等的数组进行操作。最重要的是,您不需要方括号,并且在引号之间引用字符串。另外,不确定 compute
是否真的像那样工作,混合了 if
转换。
您想解析一个变量列表,检查 3 个字符串,并在满足条件时进行转换:
NUMERIC citrus (f1.0).
COMPUTE citrus=0.
DO REPEAT PICK = PICK1 to PICK7000.
IF any(PICK, "orange", "lemon", "clementine") citrus=1.
END REPEAT.
EXECUTE.
或者,更合成一点:
NUMERIC citrus (f1.0).
DO REPEAT PICK = PICK1 to PICK7000.
COMPUTE citrus=any(PICK, "orange", "lemon", "clementine").
END REPEAT.
EXECUTE.
或者,如果您还想查看任何“浆果”:
NUMERIC citrus (f1.0).
NUMERIC berries (f1.0).
DO REPEAT PICK = PICK1 to PICK7000.
COMPUTE citrus=any(PICK, "orange", "lemon", "clementine").
COMPUTE berries = CHAR.INDEX(PICK, "berry")>0.
END REPEAT.
EXECUTE.
我想使用针列表遍历多个变量(即多个干草堆)。
数据:
PICK1 | PICK2 | PICK3 | PICK4 |
---|---|---|---|
Grape | Raspberry | Clementime | Strawberry |
Strawberry | Lemon | Blueberry | Apple |
Cantelope | Mango | Grape | Pear |
Apple | Orange | Kiwi | Raspberry |
有效的手动代码
Compute citrus=0
if char.index(PICK1,"orange")>0 citrus=1.
if char.index(PICK2,"orange")>0 citrus=1.
if char.index(PICK3,"orange")>0 citrus=1.
if char.index(PICK4,"orange")>0 citrus=1.
if char.index(PICK1,"lemon")>0 citrus=1.
if char.index(PICK2,"lemon")>0 citrus=1.
if char.index(PICK3,"lemon")>0 citrus=1.
if char.index(PICK4,"lemon")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
if char.index(PICK4,"clementine")>0 citrus=1.
期望的结果:如果在变量中找到值,则记录 1
Citrus |
---|
1 |
1 |
0 |
1 |
尝试的代码(不工作)
do repeat L = [orange, lemon, clementine]
/PICK = PICK1 to PICK7000.
compute L = if char.index(PICK,L)>0 citrus=1.
end repeat.
我最初使用 char.index 来搜索具有相似基本名称的多个值。例如,搜索“berry”会希望找到“strawberry”、“blueberry”等。有没有办法将其合并到 COMPUTE
函数中?
NUMERIC berry (f1.0).
DO REPEAT PICK = PICK1 to PICK7000.
COMPUTE berry=any(PICK, char.index(PICK, "berry").
END REPEAT.
EXECUTE.
do repeat
结构使用长度必须相等的数组进行操作。最重要的是,您不需要方括号,并且在引号之间引用字符串。另外,不确定 compute
是否真的像那样工作,混合了 if
转换。
您想解析一个变量列表,检查 3 个字符串,并在满足条件时进行转换:
NUMERIC citrus (f1.0).
COMPUTE citrus=0.
DO REPEAT PICK = PICK1 to PICK7000.
IF any(PICK, "orange", "lemon", "clementine") citrus=1.
END REPEAT.
EXECUTE.
或者,更合成一点:
NUMERIC citrus (f1.0).
DO REPEAT PICK = PICK1 to PICK7000.
COMPUTE citrus=any(PICK, "orange", "lemon", "clementine").
END REPEAT.
EXECUTE.
或者,如果您还想查看任何“浆果”:
NUMERIC citrus (f1.0).
NUMERIC berries (f1.0).
DO REPEAT PICK = PICK1 to PICK7000.
COMPUTE citrus=any(PICK, "orange", "lemon", "clementine").
COMPUTE berries = CHAR.INDEX(PICK, "berry")>0.
END REPEAT.
EXECUTE.