Oken
Oken

Reputation: 53

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)?

Upvotes: 3

Views: 977

Answers (4)

charles
charles

Reputation: 19

Fill ! V2 you need to call V before rv, V2 is positional, that is command is read only after changes has been made. Define filldown ! v5 do! rv! fill! V2 .......... End

Upvotes: -1

charles
charles

Reputation: 19

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

Upvotes: 0

eli-k
eli-k

Reputation: 11310

@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.

Upvotes: 3

horace_vr
horace_vr

Reputation: 3166

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.

Upvotes: 2

Related Questions