SPSS - 通过操纵另一个参数为宏参数赋值
SPSS - assign a value to a macro argument by manipulating another argument
我正在尝试构建一个带有 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.
关于解决此问题的任何建议?
如果您查看 Macro Directives (DEFINE-!ENDDEFINE command) documentation, you'll find in the String Manipulation 部分,您需要 !SUBSTR
进行此类替换。
鉴于 DEFINE/!ENDDEFINE 中没有 "REPLACE" 字符串操作函数,您将不得不使用各种其他函数的组合,因此您可能也会发现此 link 有用,进行算术运算。
(为此,我已经停止使用 SPSS MACRO 语言进行编码,因为它非常、非常难看,并且考虑到在 SPSS 中使用 Python,我现在更喜欢使用 Python 这将是非常微不足道的事情)。
"-" 作为特殊字符在宏解析器中将字符串划分为单独的标记。因此,您无法使用 !TOKENS(1) 读取包含“-”作为宏参数的字符串(就像您在示例中所做的那样)。
这在其他情况下可能会导致问题,但在这里我们可以将错误变成一个功能:以下宏运行 !ARG1 中的单独标记,并将“-”替换为“_”。如果没有“-”,则!ARG1中只有一个token,什么都不会改变。
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_.
我正在尝试构建一个带有 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.
关于解决此问题的任何建议?
如果您查看 Macro Directives (DEFINE-!ENDDEFINE command) documentation, you'll find in the String Manipulation 部分,您需要 !SUBSTR
进行此类替换。
鉴于 DEFINE/!ENDDEFINE 中没有 "REPLACE" 字符串操作函数,您将不得不使用各种其他函数的组合,因此您可能也会发现此 link 有用,进行算术运算。
(为此,我已经停止使用 SPSS MACRO 语言进行编码,因为它非常、非常难看,并且考虑到在 SPSS 中使用 Python,我现在更喜欢使用 Python 这将是非常微不足道的事情)。
"-" 作为特殊字符在宏解析器中将字符串划分为单独的标记。因此,您无法使用 !TOKENS(1) 读取包含“-”作为宏参数的字符串(就像您在示例中所做的那样)。
这在其他情况下可能会导致问题,但在这里我们可以将错误变成一个功能:以下宏运行 !ARG1 中的单独标记,并将“-”替换为“_”。如果没有“-”,则!ARG1中只有一个token,什么都不会改变。
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_.