Search code examples
stringreplacemacrosspss

SPSS Macro - String Manipulation in DEFINE - !ENDDEFINE


According to the SPSS documentation-link below I understand that !REPLACE is not a valid string manipulation function with DEFINE - !ENDDEFINE. Surely there must be a work around for this...either with other string manipulation functions, or I think more likely another approach. What have you guys seen in the past for this kind of problem? Thanks!

Link to documentation: https://www.ibm.com/support/knowledgecenter/SSLVMB_21.0.0/com.ibm.spss.statistics.help/syn_define_string_manipulation_functions.htm

Specific Problem:

User feeds the macro variable names (vars) which
  1) need to be averaged by specified groups (groupVars),
  2) then Zscored
  3) and their Zscores averaged by the same groups.

When Zscored, the new variable names are "Z" + orig var name. So I wanted to Replace in my vars list, " " with " z" and put a z at the very beginning with !CONC. Problem is string manip functions here do not seem to allow !REPLACE.

Example Code:

 DEFINE !RunProfiles (vars=!CHAREND ('/') /groupVars =!CMDEND) 

*GET MEANS FOR EACH GROUPVAR FOR SPECIFIED VARIABLES.
!DO !I !IN (!groupVars)
    means tables=!vars by !I / cells mean.
!DOEND

*GET ZSCORES FOR SPECIFIED VARS.
DESCRIPTIVES VARIABLES=!profvars
  /SAVE
  /STATISTICS=MEAN.

*GET Z MEANS FOR EACH SEGMENT FOR SPECIFIED VARIABLES.
!DO !I !IN (!groupVars)
    !LET Zvars = !CONC("z", !REPLACE(!vars, " ", " z"))
   means tables=!Zvars by !I / cells mean.
!DOEND

!ENDDEFINE.

This is the problematic line: !LET Zvars = !CONC("z", !REPLACE(!vars, " ", " z"))


Solution

  • I think what you need is instead of your problematic line is this:

    *GET Z MEANS FOR EACH SEGMENT FOR SPECIFIED VARIABLES.
    !DO !I !IN (!groupVars)
        means tables=!do !k !in(!vars) !concat("Z",!k) !doend by !I / cells mean.
    !DOEND