'Do IF and Compute in spss macros

I have the following spss syntax:

DO IF SYSMIS(V5).
 COMPUTE V5 = LAG(V5).
END IF.
EXE.

It works fine. However I'd like to repeat the same process for several variables. I tried to write a macro to achieve this but I keep getting error messages. This was my attempt:

define filldown (!positional !cmdend).
  do if sysmis(!1).
   compute !1 = lag (!1).
  end if.
  execute.
!enddefine.

!filldown V5 age wt htm.

How do I write a macro that will work (I'm new to macros)?



Solution 1:[1]

@horace_vr's do repeat solution is definitely the right approach for this case. The following is just to learn something about macros while you're at it.

First of all, you can use your present macro for each variable separately, but you need to use the original macro call (don't add "!"), so:

filldown V5.
filldown age.
....

But of course you can create a loop within the macro, like this:

define filldown (!positional !cmdend).
!do !vr !in (!1)
  do if sysmis(!vr).
   compute !vr = lag (!vr).
  end if.
  execute.
!doend
!enddefine.

Now you can use the macro call once with the complete list:

filldown V5 age wt htm.

Solution 2:[2]

The macro is simply a text substitution function. It will literally replace your !1 with whatever argument you are providing when calling the macro (V5 age wt htm).

To keep things simple, I would recommend using a simple do repeat command, instead of a macro, which may be little uncomfortable to use if you are not familiar with them

do repeat varlist=V5 age wt htm.
if sysmis(varlist) varlist=lag(varlist).
end repeat.
exe.

P.S.: If you really want to use your macro, you need to call it for each variable separately.

Solution 3:[3]

Set Auto sum To lisp. Other variables cannot be quantified. A quantifier is a lisp command which shows the next sum of the program.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eli-k
Solution 2 horace_vr
Solution 3