1
votes

I am fairly new to SPSS. I am trying to write a macro where "time" will be passed as an argument. I want to recode it into another macro variable "t2". I can do it in the following way:

DEFINE !my_macro (time = !TOKEN(1))
!LET !t2=" ".
!IF (!time >=2 & !time <2.5) !THEN !LET !t2=1. !IFEND.
!IF (!time >=2.5 & !time <3) !THEN !LET !t2=2. !IFEND.
!IF (!time >=3 & !time <3.5) !THEN !LET !t2=3. !IFEND.
...

The problem is I have so many of these intervals. Just wondering if there is any one line of code that will recode a macro variable into another macro variable?

1
first of all, it is not clear if t2 is string or integer; second of all - you can always use a text editor (like Notepad++) or a spreadhseet to concatenate SPSS syntax, as long as there is a pattern in your intervals' limits. - horace_vr

1 Answers

0
votes

I think a visual binning (RECODE) would work better instead of using macros. Given that your time variable is numeric.

*Create the macro for binning.
define group (!positional !cmdend).
  !do !var !in (!1)
    recode !var (lo thru 2.5=1) (2.5 thru 3.5=2) (3.5 thru hi=3) .
    value !var 1 '2-<2.5' 2 '2.5-<3.5' 3 '>=3.5'.
  exec.
 !doend
!enddefine.

group time time2 time3.  /*Conduct the binning.
list  time time2 time3.

I think that should work provided your time variable is numeric.

You could also use the DO REPEAT method.