Frage

I don't think SPSS macros can return values, so instead of assigning a value like VIXL3 = !getLastAvail target=VIX level=3 I figured I need to do something like this:

/* computes last available entry of target at given level */
define !compLastAvail(name !Tokens(1) /target !Tokens(1) /level !Tokens(1))
   compute tmpid= $casenum.
   dataset copy tmpset1.
   select if not miss(!target).
   compute !name= lag(!target, !level).
   match files /file= * /file= tmpset1 /by tmpid.
   exec.
   delete variables tmpid.
   dataset close tmpset1.
!enddefine.

/* compute last values */

!compLastAvail name="VIXCL3" target=VIXC level=3.

The compute !name = ...is where the problem is.

How should this be done properly? The above returns:

>Error # 4285 in column 9.  Text: VIXCL3
>Incorrect variable name: either the name is more than 64 characters, or it is
>not defined by a previous command.
>Execution of this command stops.
War es hilfreich?

Lösung

When you pass tokens to the macro, they get interpreted literally. So when you specify

!compLastAvail name="VIXCL3"

It gets passed to the corresponding compute statement as "VIXCL3", instead of just a variable name without quotation marks (e.g. VIXCL3).

Two other general pieces of advice;

  1. If you do the command set mprint on before you execute your macro, you will see how your tokens are passed to the macro. In this instance, if you had taken that step, you would have seen that the offending compute statement and error message.

  2. Sometimes you do what to use quotation marks in tokens, and when that is the case the string commands !QUOTE and !UNQUOTE come in handy.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top