Macros SPSS-通过操纵另一个参数为宏参数赋值

Macros SPSS-通过操纵另一个参数为宏参数赋值,macros,spss,Macros,Spss,我正在尝试构建一个包含2个参数的宏: -调用宏时,其中一个正在被传递, -第二个是第一个的转变。 基本上,我正在尝试对第一个参数进行字符串转换,并使用结果运行一些语法 Define !MyMacro (arg1=!tokens (1), arg2=!DEFAULT(SomeValue) !tokens(1)) /*what I am trying to achieve is to replace all "-" in arg1 with "_", but the syn

我正在尝试构建一个包含2个参数的宏: -调用宏时,其中一个正在被传递, -第二个是第一个的转变。 基本上,我正在尝试对第一个参数进行字符串转换,并使用结果运行一些语法

    Define !MyMacro (arg1=!tokens (1), arg2=!DEFAULT(SomeValue) !tokens(1))
    /*what I am trying to achieve is to 
    replace all "-" in arg1 with "_", but the syntax does not work with macro arguments:
    compute !arg2 = replace(!arg1,"-","_").

    /*I need arg 2 to be available further down the syntax, as a variable name:
    fre !arg2.
    !Enddefine.
有没有关于如何解决这个问题的建议

如果您查看文档,您会在章节中发现它是
!SUBSTR
,您需要进行此类更换

给定DEFINE/中没有“REPLACE”字符串操作函数!ENDDEFINE您将不得不使用各种其他函数的组合,因此您可能会发现这对执行算术也很有用

(我已经停止使用SPSS宏语言为此目的进行编码,因为它非常非常难看,并且考虑到SPSS中使用Python,我现在更喜欢用Python进行编码,这将是非常简单的事情)。

“-”作为一个特殊字符,在宏解析器中将字符串划分为单独的标记。因此,不能使用!令牌(1)(就像您在示例中所做的那样)。
在其他情况下,这可能会导致问题,但在这里,我们可以将错误转化为一个功能:下面的宏在中通过单独的标记运行!ARG1并将“-”替换为“\u1”。如果没有“-”,则中只有一个令牌!ARG1,则不会有任何更改

Define !MyMacro (arg1=!cmdend)
!let !arg2=""
!do !i !in(!arg1)
!if (!i="-") !then !let !arg2=!concat(!arg2,"_") !else !let !arg2=!concat(!arg2,!i) !ifend
!doend.
title !quote( !arg2).
freq !arg2 .
!enddefine.

!MyMacro arg1=turn-into_.
上一个宏只能处理“-”和类似的特殊字符,下面的宏可以用于任何字符(尽管我仍然将其设置为“-”):

Define !MyMacro (arg1=!cmdend)
!let !arg2=""
!do !i = 1 !to !length(!arg1)
!if (!substr(!arg1,!i,1)="-") !then !let !arg2=!concat(!arg2,"_") !else !let !arg2=!concat(!arg2,!substr(!arg1,!i,1)) !ifend
!doend.
title !quote(!arg2).
freq !arg2 .
!enddefine.

!MyMacro arg1=turn-into_.