I am trying to create a new variable that reflects the score obtained on the last assessment. Currently, participants may have taken their last assessment in different months, with each month having been entered as a separate variable. I would like to pull from the respective month so that this new variable reflects the score from the last assessment taken, regardless which month it was taken. Any advice? I'm sure this has come up in previous questions on the list, but I haven't had much luck in finding it in the archive. Thank you in advance.
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
I've made some assumptions about your data and what you are trying to do here, as a way of demonstrating a potential solution. **************************************. set seed = 10. input program. loop #i = 1 to 100. compute case = #i. end case. end loop. end file. end input program. dataset name sim. vector Month(12, F1.0). do repeat Month = Month1 to Month12. compute Month = TRUNC(RV.UNIFORM(1,100)) * rnd(RV.UNIFORM(0,1)). end repeat. **************************************. recode Month1 to Month12 (0=sysmis). do repeat month=month1 to month12. if (nvalid(month)=1) LastAssess=month. end repeat. Would there be a more efficient way to code this, such that it looped backwards from Month12 to Month1 and stopped looping for each case after first valid month found? On 10 February 2015 at 17:58, Debbie Hahs-Vaughn <[hidden email]> wrote: I am trying to create a new variable that reflects the score obtained on the last assessment. Currently, participants may have taken their last assessment in different months, with each month having been entered as a separate variable. I would like to pull from the respective month so that this new variable reflects the score from the last assessment taken, regardless which month it was taken. Any advice? I'm sure this has come up in previous questions on the list, but I haven't had much luck in finding it in the archive. Thank you in advance. |
Administrator
|
In reply to this post by Debbie Hahs-Vaughn
This is the simplest way in my opinion.
Assumes that the scores in question are contiguous. If they aren't then make it so. -- /* simulate some data for testing*/. MATRIX. SAVE TRUNC(UNIFORM(30,10)*5)/OUTFILE * / VARIABLES x01 TO x10. END MATRIX. MISSING VALUES x01 TO x10 (0). /* start */. VECTOR x=x01 TO x10. LOOP Last_Position=10 TO 1 BY -1 . IF NOT MISSING(x(Last_Position)) Last_Score=x(Last_Position). END LOOP IF NOT MISSING(x(Last_Position)). LIST.
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?" |
In reply to this post by Debbie Hahs-Vaughn
Here is one way, using DO REPEAT you can have two lists, one running behind the other (easier to show than to explain).
***********************************. DATA LIST FREE / Month1 TO Month4. BEGIN DATA 1 2 . . 1 . . 4 . 2 3 . END DATA. *Start from 2nd month. DO REPEAT M1 = Month1 TO Month3 /M2 = Month2 TO Month4. IF MISSING(M2) M2 = M1. END REPEAT. EXECUTE. ***********************************. |
Administrator
|
In reply to this post by Jignesh Sutar
"Would there be a more efficient way to code this, such that it looped backwards from Month12 to Month1 and stopped looping for each case after first valid month found?"
Yep! Loop over a VECTOR backwards (BY -1) and terminate END LOOP IF valid value. See my solution posted in this thread.
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
Kirill Orlov made the following suggestion/amendment to me off list. VECTOR x=x01 TO x10. LOOP Last_Position=10 TO 1 BY -1 . END LOOP IF NOT MISSING(x(Last_Position)). COMPUTE Last_Score=x(Last_Position). Omitting the double IF... I guess one last thing might be to eliminate the hardcoded 10. VECTOR x=x01 TO x10. LOOP Last_Position=SUM(NVALID(x01 TO x10),NMISS(x01 TO x10) ) TO 1 BY -1 . END LOOP IF NOT MISSING(x(Last_Position)). COMPUTE Last_Score=x(Last_Position). ----
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?" |
Huh?! I just got my head around the original VECTOR/LOOP and now I'm bamboozled again. How does that work with the COMPUTE being outside the LOOP.
I haven't used VECTOR/LOOP combo much, if at all.
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
On Wednesday, 11 February 2015, David Marso <[hidden email]> wrote: Kirill Orlov made the following suggestion/amendment to me off list. |
Administrator
|
The loop is iterated until the condition is true (some value towards the end not missing) At that point the loop is exited with a valid value of Last_Position. The final Compute just grabs the vector value at the index Last_Position. Twisted eh?? Pirate laugh!! ;-))))
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?" |
If the NMISS( ..) condition is never satisfied, then the loop continues until ...
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
the step after "1"? ... when LastPosition is 0? That would be an invalid reference for x(LastPosition). That is weak code if that is how SPSS treats the index, since it depends on the data always having at least one valid value. How *does* SPSS treat the index? I've been paranoid about what happens to an index after its loop ends, ever since I learned that the (early) Fortran standards did not require any specified result. Leaving the next value was (I think) most common. -- Rich Ulrich > Date: Wed, 11 Feb 2015 16:16:39 -0700 > From: [hidden email] > Subject: Re: creating 'last available' score from multiple measurement occasions > To: [hidden email] > > The loop is iterated until the condition is true (some value towards the end > not missing) > At that point the loop is exited with a *valid value * of Last_Position. > The final Compute just grabs the vector value at the index Last_Position. > Twisted eh?? Pirate laugh!! ;-)))) > > > Jignesh Sutar wrote > > Huh?! I just got my head around the original VECTOR/LOOP and now I'm > > bamboozled again. How does that work with the COMPUTE being outside the > > LOOP. > > > > I haven't used VECTOR/LOOP combo much, if at all. > > > > On Wednesday, 11 February 2015, David Marso < > > > david.marso@ > > > > wrote: > > > >> Kirill Orlov made the following suggestion/amendment to me off list. > >> > >> VECTOR x=x01 TO x10. > >> LOOP Last_Position=10 TO 1 BY -1 . > >> END LOOP IF NOT MISSING(x(Last_Position)). > >> COMPUTE Last_Score=x(Last_Position). > >> > >> Omitting the double IF... > >> > >> I guess one last thing might be to eliminate the hardcoded 10. > >> VECTOR x=x01 TO x10. > >> LOOP Last_Position=SUM(NVALID(x01 TO x10),NMISS(x01 TO x10) ) TO 1 BY -1 > >> . > >> END LOOP IF NOT MISSING(x(Last_Position)). > >> COMPUTE Last_Score=x(Last_Position). > >> > >> > >> ---- > >> > >> > >> David Marso wrote > >> > This is the simplest way in my opinion. > >> > Assumes that the scores in question are contiguous. > >> > If they aren't then make it so. > >> > -- > >> > /* simulate some data for testing*/. > >> > MATRIX. > >> > SAVE TRUNC(UNIFORM(30,10)*5)/OUTFILE * / VARIABLES x01 TO x10. > >> > END MATRIX. > >> > MISSING VALUES x01 TO x10 (0). > >> > > >> > /* start */. > >> * > >> > VECTOR x=x01 TO x10. > >> > LOOP Last_Position=10 TO 1 BY -1 . > >> > IF NOT MISSING(x(Last_Position)) Last_Score=x(Last_Position). > >> > END LOOP IF NOT MISSING(x(Last_Position)). > >> > LIST. > >> * > >> > > >> > Debbie Hahs-Vaughn wrote > >> >> I am trying to create a new variable that reflects the score obtained > >> on > >> >> the last assessment. Currently, participants may have taken their > >> last > >> >> assessment in different months, with each month having been entered as > >> a > >> >> separate variable. I would like to pull from the respective month so > >> >> that this new variable reflects the score from the last assessment > >> taken, > >> >> regardless which month it was taken. Any advice? I'm sure this has > >> come > >> >> up in previous questions on the list, but I haven't had much luck in > >> >> finding it in the archive. Thank you in advance. > >> >> > >> >> ===================== |
Administrator
|
Rich,
Run the code. It works. If there are no valid values then you simply get a warning. No harm done. Can you provide a better solution? David
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?" |
Solutions:
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
For a short loop and short assignment statement, there is little waste of time in making the assignment before the test - as you originally wrote it. Otherwise, you make sure that the test will be met. I've had the circumstance where it was convenient to put an extra value to be tested last. If it a time-consuming assignment routine, you can simply save the index and skip the assignment until after you've exited the loop (and tested that an index exists). -- Rich Ulrich > Date: Thu, 12 Feb 2015 09:11:23 -0700 > From: [hidden email] > Subject: Re: creating 'last available' score from multiple measurement occasions > To: [hidden email] > > Rich, > Run the code. It works. > If there are no valid values then you simply get a warning. No harm done. > Can you provide a better solution? > David > > Rich Ulrich wrote > > If the NMISS( ..) condition is never satisfied, then the loop continues > > until ... > > the step after "1"? ... when LastPosition is 0? That would be an > > invalid > > reference for x(LastPosition). That is weak code if that is how SPSS > > treats > > the index, since it depends on the data always having at least one valid > > value. > > > > How *does* SPSS treat the index? > > > > I've been paranoid about what happens to an index after its loop ends, > > ever since > > I learned that the (early) Fortran standards did not require any specified > > result. > > Leaving the next value was (I think) most common. > > > > -- > > Rich Ulrich > > > >> Date: Wed, 11 Feb 2015 16:16:39 -0700 > >> From: > > > david.marso@ > > >> Subject: Re: creating 'last available' score from multiple measurement > >> occasions > >> To: > > > SPSSX-L@.UGA > > >> > >> The loop is iterated until the condition is true (some value towards the > >> end > >> not missing) > >> At that point the loop is exited with a *valid value * of Last_Position. > >> The final Compute just grabs the vector value at the index Last_Position. > >> Twisted eh?? Pirate laugh!! ;-)))) > >> > >> > >> Jignesh Sutar wrote > >> > Huh?! I just got my head around the original VECTOR/LOOP and now I'm > >> > bamboozled again. How does that work with the COMPUTE being outside the > >> > LOOP. > >> > > >> > I haven't used VECTOR/LOOP combo much, if at all. > >> > > >> > On Wednesday, 11 February 2015, David Marso < > >> > >> > david.marso@ > >> > >> > > wrote: > >> > > >> >> Kirill Orlov made the following suggestion/amendment to me off list. > >> >> > >> >> VECTOR x=x01 TO x10. > >> >> LOOP Last_Position=10 TO 1 BY -1 . > >> >> END LOOP IF NOT MISSING(x(Last_Position)). > >> >> COMPUTE Last_Score=x(Last_Position). > >> >> > >> >> Omitting the double IF... > >> >> > >> >> I guess one last thing might be to eliminate the hardcoded 10. > >> >> VECTOR x=x01 TO x10. > >> >> LOOP Last_Position=SUM(NVALID(x01 TO x10),NMISS(x01 TO x10) ) TO 1 BY > >> -1 > >> >> . > >> >> END LOOP IF NOT MISSING(x(Last_Position)). > >> >> COMPUTE Last_Score=x(Last_Position). > >> >> > >> >> > >> >> ---- > >> >> > >> >> > >> >> David Marso wrote > >> >> > This is the simplest way in my opinion. > >> >> > Assumes that the scores in question are contiguous. > >> >> > If they aren't then make it so. > >> >> > -- > >> >> > /* simulate some data for testing*/. > >> >> > MATRIX. > >> >> > SAVE TRUNC(UNIFORM(30,10)*5)/OUTFILE * / VARIABLES x01 TO x10. > >> >> > END MATRIX. > >> >> > MISSING VALUES x01 TO x10 (0). > >> >> > > >> >> > /* start */. > >> >> * > >> >> > VECTOR x=x01 TO x10. > >> >> > LOOP Last_Position=10 TO 1 BY -1 . > >> >> > IF NOT MISSING(x(Last_Position)) Last_Score=x(Last_Position). > >> >> > END LOOP IF NOT MISSING(x(Last_Position)). > >> >> > LIST. > >> >> * > >> >> > > >> >> > Debbie Hahs-Vaughn wrote > >> >> >> I am trying to create a new variable that reflects the score > >> obtained > >> >> on > >> >> >> the last assessment. Currently, participants may have taken their > >> >> last > >> >> >> assessment in different months, with each month having been entered > >> as > >> >> a > >> >> >> separate variable. I would like to pull from the respective month > >> so > >> >> >> that this new variable reflects the score from the last assessment > >> >> taken, > >> >> >> regardless which month it was taken. Any advice? I'm sure this > >> has > >> >> come > >> >> >> up in previous questions on the list, but I haven't had much luck > >> in > >> >> >> finding it in the archive. Thank you in advance. > >> >> >> > >> >> >> ===================== |
Free forum by Nabble | Edit this page |