开发者

Do a predefined loop consisting of 4 variables 100 times

I am pretty new at SPSS macro's, but I think I need one.

I have 400 variables, I want to do this loop 400 times. My variables are ordered consecutively. So first I want to do this loop for varia开发者_StackOverflow中文版bles 1 to 4, then for variables 5 to 8, then for variables 9 to 12 and so on.

vector TEQ5DBv=T0EQ5DNL to T4EQ5DNL.
loop #index = 1 to 4. 
+ IF( MISSING(TEQ5DBv(#index+1))) TEQ5DBv(#index+1) = TEQ5DBv(#index) . 
end loop. 
EXECUTE.


Below is an example of what it appears to me you are trying to do. Note I replaced your use of the looping and index with a do repeat command. To me it is just more clear what you are doing by making two lists in the do repeat command as opposed to calling lead indexes in your loop.

*making data.
DATA LIST FIXED /X1 to X4 1-4.
BEGIN DATA
1111
0101
1 0  

END DATA.

*I make new variables, so you dont overwrite your original variables.
vector X_rec (4,F1.0).
do repeat X_rec = X_rec1 to X_rec4 / X = X1 to X4.
compute X_rec = X.
end repeat.
execute.

do repeat X_later = X_rec2 to X_rec4 / X_early = X1 to X3.
if missing(X_later) = 1 X_later = X_early.
end repeat.
execute.

A few notes on this. Previously your code was overwriting your initial variables, in this code I create a set a new variables named "X_rec1 ... X_rec4", and then set those values to the same as the original set of variables (X1 to X4). The second do repeat command fills in the recoded variables if a missing value occurs with the previous variable. One big difference between this and your prior code, in your prior code if you ran it repeatedly it would continue to fill in the missing data, whereas my code would not. If you want to continue to fill in the missing data, you would just have to replace in the code above X_early = X1 to X3 with X_early = X_rec1 to X_rec3 and then just run the code at least 3 times (of course if you have a case with all missing data for the four variables, it will all still be missing.) Below is a macro to simplify calling this repeated code.

SET MPRINT ON.
DEFINE !missing_update (list = !TOKENS(1)).

!LET !list_rec = !CONCAT(!list,"_rec")

!LET !list_rec1 = !CONCAT(!list_rec,"1")
!LET !list_rec2 = !CONCAT(!list_rec,"2")
!LET !list_rec4 = !CONCAT(!list_rec,"4")

!LET !list_1 = !CONCAT(!list,"1")
!LET !list_3 = !CONCAT(!list,"3")
!LET !list_4 = !CONCAT(!list,"4")

vector !list_rec (4,F1.0).
do repeat UpdatedVar = !list_rec1 to !list_rec4 / OldVar = !list_1 to !list_4.
compute UpdatedVar = OldVar.
end repeat.
execute.

do repeat UpdatedVar = !list_rec2 to !list_rec4  / OldVar = !list_1 to !list_3.
if missing(UpdatedVar) = 1 UpdatedVar = OldVar.
end repeat.
execute.

!ENDDEFINE.

*dropping recoded variables I made before.
match files file = *
/drop X_rec1 to X_rec4.
execute.

!missing_update list = X.

I suspect there is a way to loop through all of the variables in the dataset without having to call the macro repeatedly for each set, but I'm not sure how to do it (it may not be possible within DEFINE, and you may have to resort to writing up a python program). Worst case you just have to write the above macro defined function 400 times!


  1. Your Loop-Syntax is incorrect because when #index reaches "4" your code says that you want to do an operation on TEQ5DBv(5). So you definetly will get an error.

I don't know what exactly you want to do, but a nested loop might help you to achieve your goal.

Here is an example:

* Creating some Data.
DATA LIST FIXED /v1 to v12 1-12.
BEGIN DATA
1234    9012
 2 4 6 8 1 2
1 3 5 7 9 1 
12  56  90  
   456   012
END DATA.

* Vectorset of variables
VECTOR vv = v1 TO v12.

LOOP #i = 1 TO 12 BY 4.
   LOOP #j = 0 TO 2.     /* inner Loop runs only up to "2" so you wont exceed your inner block.
      IF(MISSING(vv(#i+#j+1))) vv(#i+#j+1) = vv(#i+#j).
   END LOOP.
END LOOP.
EXECUTE.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜