Hello! 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. Who can help me ? So first I want to do this loop for variables 1 to 4, than for variables 5 to 8, than for variables 8 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 . Thank you very much! Greetings maartje |
|
Administrator
|
In reply to this post by maartjegoorden
Does this do what you want?
vector TEQ5DBv=T0EQ5DNL to LastVar. /* LastVar = name of 400th variable. loop #i = 0 to 396 by 4. loop #j = 1 to 4. + compute #index = #i + #j. + IF( MISSING(TEQ5DBv(#index+1))) TEQ5DBv(#index+1) = TEQ5DBv(#index) . end loop . end loop. EXECUTE .
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
In reply to this post by maartjegoorden
Question: Say your vars are called V001 TO V400 rather than what you are calling them (no way to know on our end what to call them).
If V005 is missing then do you wish to replace it with the value of V004? If so then why are you breaking it up into ...variables 1 to 4, than for variables 5 to 8 (just Loop from 1 to 400)? If not then what value should the first variable in each of the 100 sets take?
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
In reply to this post by Bruce Weaver
Doesn't this do the same thing as:
LOOP #=1 To 400. If MISSING(TEQ5DBv(#+1) ) TEQ5DBv(#+1)=TEQ5DBv(#). END LOOP. Check boundary conditions -It will run off the edge when #=400-
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
In reply to this post by Albert-Jan Roskam
WOW! This is so much easier to understand than the simple VECTOR/LOOP ;-)
GACK! Albert-Jan. Sometimes Python has its place (eg requiring dictionary info/data cursors etc), but this is probably one of those everything looks like a nail when you have a hammer situations. >BEGIN PROGRAM. >import os >import spss >sep = os.linesep >cmds = "" >for n in range(0, 400): > if n > 0 and n % 5 != 0: > cmd = "IF( MISSING(T%dEQ5DNL) ) T%dEQ5DNL = T%dEQ5DNL.%s" % (n, n, n-1, sep) > else: > cmd = "*" + 50 * "-" + ".%s* New block (%d-%d).%s" % (sep, n, n+4, sep) + "*" + 50 * "-" + >".%s" % sep > cmds += cmd >spss.Submit(cmds) >END PROGRAM.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
|
Administrator
|
In reply to this post by David Marso
Yes, of course it does. I don't suppose you'd believe I was just testing you, would you? :-|
But that makes me wonder what the OP was getting at when they said, "...first I want to do this loop for variables 1 to 4, than for variables 5 to 8, than for variables 8 to 12 and so on." Does that mean that the loop should only look at variable(#+1) provided # is less than 4? I.e., they want to carry values forward, but only within groups of 4 variables. Something like: data list list / v1 to v8 (8f2.0). begin data 1 2 3 4 1 2 3 4 1 2 3 9 1 2 3 9 1 2 9 9 1 2 9 9 1 9 9 9 1 9 9 9 1 2 3 4 9 2 3 4 9 2 3 4 9 2 3 4 end data. missing values all(9). list. /* Before processing. vector v = v1 to v8. loop #i = 1 to 8. - compute #TheLastVar = mod(#i,4) EQ 0. - DO IF not #TheLastVar. - IF ( MISSING(v(#i+1))) v(#i+1) = v(#i) . - END IF. end loop . EXECUTE . list. /* After processing. Output before processing (9 is Missing): v1 v2 v3 v4 v5 v6 v7 v8 1 2 3 4 1 2 3 4 1 2 3 9 1 2 3 9 1 2 9 9 1 2 9 9 1 9 9 9 1 9 9 9 1 2 3 4 9 2 3 4 9 2 3 4 9 2 3 4 Output after processing: v1 v2 v3 v4 v5 v6 v7 v8 1 2 3 4 1 2 3 4 1 2 3 3 1 2 3 3 1 2 2 2 1 2 2 2 1 1 1 1 1 1 1 1 1 2 3 4 9 2 3 4 9 2 3 4 9 2 3 4 Notice that the value of v5 (the first variable in the 2nd group of 4) remains missing, rather than having the valid values from v4 carried forward. This now makes me wonder if the OP is trying to to "last observation carried forward" imputation (LOCF). If so, that method is not highly regarded these days. There are many articles on it, including a very readable one by David Streiner. http://ebmh.bmj.com/content/11/1/3.2.short HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
"I don't suppose you'd believe I was just testing you, would you? :-|"
Sure.... ;-) "There are many articles on it, including a very readable one by David Streiner. http://ebmh.bmj.com/content/11/1/3.2.short " That looks interesting (the abstract that is). Wish I had access to it. (Lucky you academics are possible to email me the pdf?). -- Assuming that the 'imputation' not cross boundaries something like... A little more to the point (I try to code directly to the variable being examined.) in this case we are interested in examining and doing something with a variable in a given position. Rather than referencing this as #+1, I prefer to use # and then do replacement on #-1 (note loop begins with 2 rather than 1). --- VECTOR V=Var001 TO Var400. LOOP #=2 To 400. IF ( (MOD(#,4) NE 1) AND (MISSING(V(#) ) V(#)=V(#-1). END LOOP.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
Yep, that's a bit neater. Thanks David. It's still not clear whether that's what the OP wanted, of course. ;-)
I'll send you that Streiner article. bw
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Thank you for all your responses, I didn't see them, I thought I would get a notice but I didn't. Eventually I found out myself, that LOCF was not a good imputation method (I tried to do that yeah) But thank you all!!
Greetings maartje |
Free forum by Nabble | Edit this page |