SPSS Statistics

SPSS Statistics

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

 View Only
Expand all | Collapse all

Creating Binary Variables from a Single Select Variable

  • 1.  Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 04:06 PM

    Hi - I am trying to create binary variables from a single select variable. I need every answer option to be recoded into 1/0 variables. While this can be done with the recode command it requires multiple recode statements and wondering if we have another command that can do this. If not, I'll proceed as planned. 

    Also, is there a way for SPSS to automatically pick up the value labels in the question. 

    thanks in advance! Ratna



    ------------------------------
    Ratna Wynn
    ------------------------------


  • 2.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 04:26 PM
    Try the SPSSINC CREATE DUMMIES extension command, which appears under Transform.  It can do many variables in a single command, and it will apply the value labels to the dummies.

    --





  • 3.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 06:43 PM

    Thanks Jon! I ran it but got no output.  Here is the syntax I ran: 


    DATASET ACTIVATE $DataSet.
    SPSSINC CREATE DUMMIES VARIABLE=B5c1 
    /OPTIONS ORDER=A USEVALUELABELS=YES USEML=YES OMITFIRST=NO.

    I got the error below. Can you please help explain what I need to do



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 4.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 06:48 PM

    I got the error No dummy variable creation was specified. 



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 5.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 08:10 PM
    I would try autorecode





  • 6.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 09:25 PM
    I don't see any error message






  • 7.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 09:31 PM
    I saw the error message in Jack's post.  You didn't specify a rootname or rootnames.  The rootname becomes the first part of the name of the generated variables.  If you specify more than one variable, you would list a rootname for each one.

    From the dialog box help...
    Root Names. Specify names to be used as the prefix of all the dummy variables for each variable selected. Enter as many names as there are variables selected, separated by a blank. The dummy names will have the form rootname_n. If a variable with that name already exists, it is replaced.

    --





  • 8.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 09:44 PM
      |   view attached

    Hi John - I get this error when I specify the root name. 



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 9.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 09:53 PM
    Again, I don't see any error message.


    --





  • 10.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 09:57 PM

    Strange - I see it below the signature line.  I get this error

    >Warning # 6894.  Command name: BEGIN PROGRAM 
    >The external program exited unexpectedly and lost its content, a new external 
    >program will startup to execute the rest of job.



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 11.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 09:59 PM
    That error is not specific to this procedure but suggests that there is a problem with the SPSS Python installation on your system.  If you run this code, you will probably see the same error.
    begin program.
    import spss
    print("running Python code")
    end program.

    I cannot tell what might be the problem here.

    --





  • 12.  RE: Creating Binary Variables from a Single Select Variable

    Posted Mon October 14, 2024 10:08 PM

    Yes - I get the same error.  

    What I want to do is write a macro that will convert each variable into binary based on a value label. Is there a way to not use a utility and do this via syntax.  I cannot figure out how to know what the value labels are and have SPSS create a separate binary variable for each value label where the value is 1 for the value label and 0 for all other values. E.g. if I have a variable Qc2 with 2 value labels 1 and 2  - I need the macro to create new variables called binary_<var name>_<value> where the first variable binary_qc2_1 where 1 = 1 and sysmis = symsis and else = 0 and binary_qc2_2 where 2 = 1, sysmis = sysmis and else = 0. I started this code but know its not correct.

    DEFINE !binary(!positional !charend('/')/ !positional !cmdend)
    !do !x !in (!1)
    recode !x (sysmis =symis) (!y= 1) into !concat("binary_",!y, !x").
    !doend
    !do !x !in (!1)
    APPLY DICTIONARY
      /FROM *
      /SOURCE VARIABLES=!x
      /TARGET VARIABLES=("binary_",!y, !x")
      /VARINFO ALIGNMENT FORMATS LEVEL ROLE MISSING VALLABELS=REPLACE ATTRIBUTES=REPLACE VARLABEL WIDTH.    
    !doend
    !ENDDEFINE.



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 13.  RE: Creating Binary Variables from a Single Select Variable

    Posted Tue October 15, 2024 03:24 PM

    I am reinstalling SPSS but are there extra steps that are needed to get the python integration - do I need to install Python separately. We have bought a large number of licenses and this is an issue with all our licenses.



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 14.  RE: Creating Binary Variables from a Single Select Variable

    Posted Tue October 15, 2024 03:37 PM
    As long as you are on 29 or 30, just installing SPSS will automatically set up the Python and R interfaces.  Do the install in Admin mode, but you don't need that mode just to run SPSS afterwards.

    --





  • 15.  RE: Creating Binary Variables from a Single Select Variable

    Posted Tue October 15, 2024 03:44 PM
      |   view attached

    hi - I am with IT and we just installed V30 on my machine.  I did run the syntax you shared to see if python is working and it did run the python code (see attached file with screenshot). But when I run the create dummies it does not work. 

    Here is the code I ran

    DATASET ACTIVATE $DataSet.
    SPSSINC CREATE DUMMIES VARIABLE=B5c1 
    ROOTNAME1=Binary_ 
    /OPTIONS ORDER=A USEVALUELABELS=YES USEML=YES OMITFIRST=NO.

    When I look at the variables there are no variables with the pre-fix Binary_. Is there something wrong with my syntax or is Python still not working. 



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 16.  RE: Creating Binary Variables from a Single Select Variable

    Posted Wed October 16, 2024 10:48 AM

    I just saw that my reply last night didn't post.

    What I said...

    This is really strange.  Are you running from the syntax window or the dialog box directly?  If syntax, try justusing ok in the dialog box to rule out some other possibilities.  You might also run an OMSEND comma d from the SE to make sure that no output is suppressed.



    ------------------------------
    Jon Peck
    Data Scientist
    JKP Associates
    Santa Fe
    ------------------------------



  • 17.  RE: Creating Binary Variables from a Single Select Variable

    Posted Wed October 16, 2024 10:55 AM
    Edited by Ratna Wynn Wed October 16, 2024 10:57 AM

    I ran it both ways and had a colleague also run it both ways. The command runs, no errors, but there is no output and no new variables. No output is suppressed. It is a mystery. I am going to try the macro Kirill shared today and hopefully that will work. Thanks for your help



    ------------------------------
    Ratna Wynn
    ------------------------------



  • 18.  RE: Creating Binary Variables from a Single Select Variable
    Best Answer

    Posted Tue October 15, 2024 04:18 AM
    Edited by NICK PLOWDEN Mon November 11, 2024 01:40 PM

    You may use macro !KO_ACATBIN or !KO_ACATBIN2 from "Categorical-binary recodings" collection on "Kirill's SPSS macros page" site.



    ------------------------------
    Kirill Orlov
    ------------------------------



  • 19.  RE: Creating Binary Variables from a Single Select Variable

    Posted Tue October 15, 2024 03:40 PM

    Thank you Kirill!



    ------------------------------
    Ratna Wynn
    ------------------------------