SPSS Statistics

SPSS Statistics

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

 View Only
  • 1.  How to use IF statement to create new variable?

    Posted Fri May 14, 2021 02:11 PM
    Hi, 
    Hope you are well.

    I just joined this forum so I did not quite know how this works. Please let me know if I was in the wrong forum group. Anyway, many thanks for help.

    My question is:
    I have 3 variables, each of them has the following value:
    Var A: 1,2
    Var B:1,2
    Var C: 1,2,3,4,5,6,7,8,9

    I would like to use IF statement:
    IF Var A=1 OR Var B=1 OR Var C=2 OR 4 OR 5 OR 8, Then create a new variable and gives a value of 1. Otherwise give the value of 0. 

    For instance, if Var A=1, Var=2, Var C=1, then create a new variable and gives a value of 1.
    If Var A=2, Var B=2, Var C= 1, then create a new variable and gives a value of 0. 

    I was just wondering how to achieve this with SPSS? I have never used its language and quite confused.

    Many thanks and best wishes,

    Scarlett


    ------------------------------
    yi wang
    ------------------------------

    #SPSSStatistics


  • 2.  RE: How to use IF statement to create new variable?

    Posted Fri May 14, 2021 02:29 PM
    Edited by System Admin Fri January 20, 2023 04:10 PM
    1. If Var A=1 OR Var B=1 OR Var C=2 OR 4 OR 5 OR 8, Then create a new variable and gives a value of 1. Otherwise give the value of 0.

    2. If Var A=2, Var B=2, Var C= 1, then create a new variable and gives a value of 0.

    * In SPSS syntax, I would try this...

    COMPUTE NewVar=(VarA=1 | VarB=1 | ANY(VarC,2,4,5,8)).
    COMPUTE AnotherNewVar=1.
    IF (VarA=2 & VarB=2 & VarC=1) AnotherNewVar = 0.
    EXECUTE.

    * NOTE: the vertical bar | is short for OR.

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



  • 3.  RE: How to use IF statement to create new variable?

    Posted Sun May 16, 2021 04:33 AM
    Thank you so so much, this really helps!
    By the way, it did not work at first but later I found out I have too many missing values which confused my SPSS. So I recoded missing values into 0. 
    Anyway, Thank you!

    ------------------------------
    yi wang
    ------------------------------