我正在尝试构建一个带有两个参数的宏: - 调用宏时传递其中之一, - 第二个是第一个的转变。 基本上,我试图对第一个参数进行字符串转换,并使用结果来运行一些语法。
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.
有什么建议可以解决这个问题吗?
最佳答案
“-”是一个特殊字符,在宏解析器中将字符串划分为单独的标记。因此,您无法使用 !TOKENS(1) 读取包含“-”作为宏参数的字符串(就像您在示例中所做的那样)。
这在其他情况下可能会导致问题,但在这里我们可以将错误转化为功能:以下宏运行 !ARG1 中的单独标记,并将“-”替换为“_”。如果没有“-”,则!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_.
关于macros - SPSS - 通过操作另一个参数为宏参数赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39871492/