带有前导零的SPSS自动变量标签

时间:2013-08-15 13:14:32

标签: spss

我正在努力的是采用http://www.spsstools.net/中的一种语法/宏。

它旨在更改没有前导零的“多个”变量的标签,但我的变量确实有这些:

DATA LIST LIST /id.
BEGIN DATA
1 
END DATA.

NUMERIC set01sub1 TO set01sub4.
* but the intended variable names are set01sub01 TO set01sub04 (with leading zeros and going over 10).

SET MPRINT=yes.
DEFINE !label (lab=!TOKENS(1)  /stem=!TOKENS(1)  /nb1=!TOKENS(1)  /nb2=!TOKENS(1))
    !DO !cnt=!nb1 !TO !nb2
        !LET !var=!CONCAT(!stem,!cnt)
        !LET !labe=!QUOTE(!CONCAT(!UNQUOTE(!lab),!cnt))
        VARIABLE LABEL !var !labe.
    !DOEND.
!ENDDEFINE.

!label lab='Set 1, subset ' stem=set01sub nb1=1 nb2=4.

我非常天真,我试图使用!STRING(...,N2)

!LET !labe=!QUOTE(!CONCAT(!UNQUOTE(!lab),!STRING(!cnt,N2)))

但是,这没有按预期工作

我的变量是

subID
rvnAns_s01m01 TO rvnAns_s01m12
rvnAns_s02m01 TO rvnAns_s02m36
rvnAns_s03m01 TO rvnAns_s03m36

rvnEva_s01m01 TO rvnEva_s01m12
rvnEva_s02m01 TO rvnEva_s02m36
rvnEva_s03m01 TO rvnEva_s03m36

,目标标签是:

"Subject ID"


"RAPM, Series 01, Matrix 01 answer"
"RAPM, Series 01, Matrix 02 answer"
...
"RAPM, Series 01, Matrix 12 answer"


"RAPM, Series 02, Matrix 01 answer"
"RAPM, Series 02, Matrix 02 answer"
...
"RAPM, Series 02, Matrix 36 answer"


"RAPM, Series 03, Matrix 01 answer"
"RAPM, Series 03, Matrix 02 answer"
...
"RAPM, Series 03, Matrix 36 answer"

"RAPM, Series 01, Matrix 01 answer evaluation"
"RAPM, Series 01, Matrix 02 answer evaluation"
...
"RAPM, Series 01, Matrix 12 answer evaluation"


"RAPM, Series 02, Matrix 01 answer evaluation"
"RAPM, Series 02, Matrix 02 answer evaluation"
...
"RAPM, Series 02, Matrix 36 answer evaluation"


"RAPM, Series 03, Matrix 01 answer evaluation"
"RAPM, Series 03, Matrix 02 answer evaluation"
...
"RAPMs, Series 03, Matrix 36 answer evaluation"

我非常感谢有关如何取得这样成果的任何帮助或建议。

2 个答案:

答案 0 :(得分:1)

如果您通过SPSS社区网站(www.ibm.com/developerworks/spssdevcentral)安装Python Essentials,以下程序将转换变量名称。

它有两个假设: 1)没有名称具有仅具有前导零的形式,例如x0y1。 (这可以通过稍微复杂一点来解决 2)所有重命名都不会导致名称冲突。 3)扩展名称都不会超过名称的最大长度(64字节)。

程序下方的说明。

begin program.
import spss, re 

for v in range(spss.GetVariableCount()):
  vname = spss.GetVariableName(v)
  vnamenew = re.sub(r"(\D)([1-9])", r"\g<1>0\g<2>", vname)
  if vname != vnamenew:
    spss.Submit("rename variables (%s=%s)" % (vname, vnamenew))
    print vname, "->", vnamenew
end program.

该程序遍历所有变量名称。对于每一个,它会查找所有出现的nondigit-nonzerodit,并用nondigit-0-digit替换它,然后生成并运行一个rename variables命令。

答案 1 :(得分:0)

正如我在crossvalidated的评论中所说,如果您提供带有前导零的stem令牌,则您的代码适用于给定的示例,例如stem=set01sub0

如果您的范围超过10位数,我认为您的10+值不会有前导零。下面我在MACRO中有一个例子,使用条件评估来连接低于10的值的前导零。如果你有更多的值(例如进入100,所以有两个前导零),这需要修改。

DATA LIST LIST /id.
BEGIN DATA
1 
END DATA.
NUMERIC set01sub01 TO set01sub15.

DEFINE !label (lab=!TOKENS(1)  /stem=!TOKENS(1)  /nb1=!TOKENS(1)  /nb2=!TOKENS(1))
    !DO !cnt=!nb1 !TO !nb2
        !IF (!LENGTH(!cnt) = 1) !THEN
            !LET !cnt0 = !CONCAT("0",!cnt)
        !ELSE
            !LET !cnt0 = !cnt
        !IFEND
        !LET !var=!CONCAT(!stem,!cnt0)
        !LET !labe=!QUOTE(!CONCAT(!UNQUOTE(!lab),!cnt0))
        VARIABLE LABEL !var !labe.
    !DOEND.
!ENDDEFINE.

PRESERVE.
SET MPRINT ON.
!label lab='Set 1, subset ' stem=set01sub nb1=1 nb2=15.
RESTORE.