2

I want to do certain actions on groups of variables. Each group has a specific index in the name. I don't want to repeat the syntax for each group. Is there a way to dynamically reference the variable names?

Below is the syntax. The 207 is the index that changes for each group of variables.

DO REPEAT aa= M9_207_1 to M9_207_99.
.....
END REPEAT.
EXECUTE.

2 Answers 2

1

You can use a macro to do this.

first define the macro:

define !MyMacro ()
!do !ndx=201 !to 207
  DO REPEAT aa= !concat("M9_",!ndx,"_1") to !concat("M9_",!ndx,"_99").
  .....
  END REPEAT.
  EXECUTE.
!doend
!enddefine.

then call it:

!MyMacro.

The macro defined here will run through indexes 201, 202, 203, etc'. If you need a more specific list of indexes, you can define the macro this way:

define !MyMacro (!pos=!cmdend)
!do !ndx !in(!1)
  DO REPEAT aa= !concat("M9_",!ndx,"_1") to !concat("M9_",!ndx,"_99").
  .....
  END REPEAT.
  EXECUTE.
!doend
!enddefine.

and then call it, giving the indexes (you have go specify each index individually):

!MyMacro 207 311 501 502 503 504 785.

Sign up to request clarification or add additional context in comments.

4 Comments

Why do you concatenate !ind? Shouldn't it be !ndx?
Okay, I tried to run the following: define !MyMacro(!pos=!cmdend) !do !ndx !in (!1) DO REPEAT aa= !concat("Q1_",!ind,"1") to !concat("Q1",!ind,"_5"). FREQ aa. END REPEAT. EXECUTE. !doend !enddefine. !MyMacro '010' '123'. To which I got the errors: >Error # 6835 in column 34. Text: , >In a macro expression, a function name was not followed by a left parenthesis. >Execution of this command stops. >Error # 4508 in column 23. Text: _ >Unrecognized text appears on the DO REPEAT command. It will be ignored. >Execution of this command stops.
1. you are right about !ind and !ndx - I now corrected the code, should run better
2. the syntax you ran has a few problems - (a) use my corrected syntax (b) you're missing an underline in !concat("Q1_",!ind,"1") (c) you can't use freq within a do repeat loop.
0

It sounds like you could do some loop operation. I saw this kind of problem in R very often. I think the same idea would work in SPSS as well. Usually, I would try this:

for i in (1:207) {
  for j in (1:99) {
    M9[i,j] = ...
    certain actions...
  }
}

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.