SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Tue February 21, 2023 10:49 AM
      |   view attached

    Hello, dear community!

     

    Having an issue with a DO REPEAT command.

     

    I have 10 variables (OP1 to OP10) that are answered on a multiple-choice test.

     

    I input (as variables) the correct answer to each OP variable and the score for that answer.

     

    I want to create a Bolian index for a true answer and SUM the scores if true.  

     

    I am using the DO REPEAT function to compact the code, but I can not SUM the score variable if the index is true! The sum of scores does not calculate.

     

    What is wrong? Should I revert to LOOP?

     

    File and Syntax are attached-

     

    compute ans1=4 .

    compute ans2=3 .

    compute ans3=2 .

    compute ans4=4 .

    compute ans5=5 .

    compute ans6=4 .

    compute ans7=2 .

    compute ans8=2 .

    compute ans9=2 .

    compute ans10=4 .

    compute score_ans1=10 .

    compute score_ans2=10 .

    compute score_ans3=10 .

    compute score_ans4=10 .

    compute score_ans5=10 .

    compute score_ans6=5 .

    compute score_ans7=5 .

    compute score_ans8=5 .

    compute score_ans9=20 .

    compute score_ans10=15 .

    EXECUTE .

     

    DO REPEAT op= op1 to op10

                          /ans= ans1 to ans10

                          /score= score_ans1 to score_ans10

                           /indx= indx1 to indx10.

    if (op=ans)  indx=1.

    if (indx>0) score_sum= sum(score).

    END REPEAT.

    EXECUTE.

     

     

    Thanks!!!!

     

     

     

     

     

     

     

     

    Meni Berger | Data scientist and Head of Tech Support E-mail    Meni@genius.co.il

    11 Menachem Begin st.,  Ramat Gan

    www.genius.co.il

    Title: LinkedIn - Description: image of LinkedIn icon

    Click here to open a support ticket  

     

     

    Attachment(s)

    sav
    op only.sav   3 KB 1 version


  • 2.  RE: DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Tue February 21, 2023 11:02 AM

    I haven't tried myself yet, but it occurs to me, did you try inserting the LEAVE command?

    if (indx>0) score_sum= sum(score).

    LEAVE score_sum.

    I'll check myself in a bit, just can't at the moment.



    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Wed February 22, 2023 03:42 AM

    Thanks, Rick.

    leave command has no effect.

    the sum function returns a value, but it seems he is ignoring the condition and only produces the last value from the score if indx equals 1.



    ------------------------------
    Meni Berger
    ------------------------------



  • 4.  RE: DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Wed February 22, 2023 11:20 AM

    How about this?

    DO REPEAT op= op1 to op10
     /ans= ans1 to ans10
     /score= score_ans1 to score_ans10
     /indx= indx1 to indx10.
    if (op=ans) indx=score.
    END REPEAT.
    compute score_sum=sum(indx1 to indx10).

    EXECUTE.



    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 5.  RE: DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Sun February 26, 2023 08:40 AM

    yes. that's the approach I took when I noticed this issue.

    but it's just overlooking the issue, ain't it? looks like a DO REPEAT malfunction to me.



    ------------------------------
    Meni Berger
    ------------------------------



  • 6.  RE: DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Mon February 27, 2023 10:48 AM
    Edited by Rick Marcantonio Mon February 27, 2023 10:48 AM

    I understand. I think LEAVE should be working. My concern is this, from the user's manual:

    • The program repeats the commands between DO REPEAT and END REPEAT once for each variable or value on the replacement list.

    I guess you could try END REPEAT PRINT and see if PRINT gives any clue. At the moment, I'm leaning toward this being a bug. I'll investigate later in the day, after meetings.



    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 7.  RE: DO REPEAT can not sum stand-in variables when using if conditions.

    Posted Mon February 27, 2023 02:23 PM

    Actually, now that I look at it, this isn't a bug at all. Using END REPEAT PRINT to show us what is actually going on, we see this:

    if (op1=ans1) indx1=1.
    if (indx1>0) score_sum= sum(score_ans1).
    if (op2=ans2) indx2=1.
    if (indx2>0) score_sum= sum(score_ans2).
    if (op3=ans3) indx3=1.
    if (indx3>0) score_sum= sum(score_ans3).
    if (op4=ans4) indx4=1.
    if (indx4>0) score_sum= sum(score_ans4).
    if (op5=ans5) indx5=1.
    if (indx5>0) score_sum= sum(score_ans5).
    if (op6=ans6) indx6=1.
    if (indx6>0) score_sum= sum(score_ans6).
    if (op7=ans7) indx7=1.
    if (indx7>0) score_sum= sum(score_ans7).
    if (op8=ans8) indx8=1.
    if (indx8>0) score_sum= sum(score_ans8).
    if (op9=ans9) indx9=1.
    if (indx9>0) score_sum= sum(score_ans9).
    if (op10=ans10) indx10=1.
    if (indx10>0) score_sum= sum(score_ans10).
    execute.

    Clearly, that's not going to work, since the SUM function is going over only one variable each invocation.

    On the other hand, the other way we both found gives:

    if             (op1=ans1) indx1=score_ans1.
    if             (op2=ans2) indx2=score_ans2.
    if             (op3=ans3) indx3=score_ans3.
    if             (op4=ans4) indx4=score_ans4.
    if             (op5=ans5) indx5=score_ans5.
    if             (op6=ans6) indx6=score_ans6.
    if             (op7=ans7) indx7=score_ans7.
    if             (op8=ans8) indx8=score_ans8.
    if             (op9=ans9) indx9=score_ans9.
    if             (op10=ans10) indx10=score_ans10.
    execute.
    compute score_sum=sum(indx1 to indx10).
    execute.

    ** which is going to do exactly what you want; within a case, it's going to go across variables.



    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------