SPSS Statistics

 View Only
Expand all | Collapse all

Efficiently creating a single variable from multiple binary variables

  • 1.  Efficiently creating a single variable from multiple binary variables

    Posted Tue December 08, 2020 12:42 PM
    Hi all,

    I have a data file in which many single response questions were asked, but in order to be compatible with accessibility software, had to be asked as a series of binary questions instead of a simple single response question. Thus, my data has been collected in a series of binary variables instead of each question as a single variable.

    This has created an extremely inefficient datafile that is difficult for others to use. I would like to collapse all of the binary variables back into single response variables. I know how to do this with syntax, but manually typing this out for every variable in my data file is going to take days and is barely worth the effort.

    Is there any way to do this efficiently?

    ------------------------------
    Sharon Morris
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Efficiently creating a single variable from multiple binary variables

    IBM Champion
    Posted Tue December 08, 2020 12:54 PM
    Try a DO REPEAT loop.  That allows you to write a COMPUTE or other transformation commands as a template where the loop substitutes the variable names.  And if the variables are contiguous, you can use TO to avoid having to list them explicitly.  If they are not contiguous but the names match some pattern, the SPSSINC SELECT VARIABLES extension command can create macros covering them.
    --





  • 3.  RE: Efficiently creating a single variable from multiple binary variables

    Posted Wed December 09, 2020 02:03 PM
    Hello  Guru Jon,

    I too have recently encountered such an issue last week. although not as many variables were present, none the less a real pain. can you please provide a proper master example of how such a DO REPEAT loop should look like?

    thanks!

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



  • 4.  RE: Efficiently creating a single variable from multiple binary variables

    IBM Champion
    Posted Wed December 09, 2020 02:54 PM
    Please be more specific about what you want to do.

    --





  • 5.  RE: Efficiently creating a single variable from multiple binary variables

    Posted Thu December 10, 2020 04:31 AM
      |   view attached
    Sure. attached is a sample SAV file. it has an MR Set with 6 dichotumius responses.
    the goal here is to transform this set into one variable in which the following are given:
    it has 3 categories 
    1- for respondents who mark '1' in any of q0033_0001, q0033_0002, q0033_0003 variables.
    2- for respondents who mark '1' in any of q0033_0004, q0033_0005, q0033_0006 variables.
    3- for respondents who mark '1' in both of the previous conditions.

    thous transforming the whole set into 3 categories within a single variable. my approach was quick a dirty:

    COMPUTE YOUNG_CAT = ANY (1, q0033_0001, q0033_0002, q0033_0003).
    COMPUTE MATURE_CAT = ANY (1, q0033_0004, q0033_0004, q0033_0006).
    EXECUTE.

    IF (YOUNG_CAT=1) AGE_CAT=1.
    IF ( MATURE_CAT=1) AGE_CAT=2.
    IF ( YOUNG_CAT=1 AND MATURE_CAT=1) AGE_CAT=3.
    EXECUTE.

    is there a more elegant way to achieve this using DO-REPEAT?

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

    Attachment(s)



  • 6.  RE: Efficiently creating a single variable from multiple binary variables

    IBM Champion
    Posted Thu December 10, 2020 10:06 AM
    I don't see any advantage to using DO REPEAT here.  You could combine all those command into one longer one, but that would be a bit harder to read.  However, I suggest that you delete those EXECUTE commands.  They are useless here and just force extra data passes.

    ------------------------------
    Jon Peck
    ------------------------------



  • 7.  RE: Efficiently creating a single variable from multiple binary variables

    Posted Sun December 13, 2020 03:51 AM
    ok then, although it does require at least one execution at the end of the sequence to run?

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



  • 8.  RE: Efficiently creating a single variable from multiple binary variables

    IBM Champion
    Posted Sun December 13, 2020 10:31 AM
    Without an EXECUTE, transformations are run "just in time".  That is, when needed by a procedure, including SAVE.  The transformations will not be visible in the Data Editor until the data are passed, but they are queued up.  EXECUTE is actually a procedure that does nothing except to pass the data.

    There are a few situations where transformations are executed out of order, generally things like data definition commands in the middle of a transformation block, and EXECUTE is needed in order to force the order, but these are rare.

    --





  • 9.  RE: Efficiently creating a single variable from multiple binary variables

    Posted Mon December 14, 2020 05:04 AM
    so you can make all those transformations without execute, and then place, let's say, a frequency command on the variables created\transformed, and it will "force" an excuse on the stored transformations?

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



  • 10.  RE: Efficiently creating a single variable from multiple binary variables

    IBM Champion
    Posted Mon December 14, 2020 09:50 AM
    Yes.  Anything that triggers a data pass will force all pending transformation to execute.

    If you are generating transformation syntax from dialog boxes, you can control whether EXECUTE is generated for each via Edit > Options > Data.

    The default setting is to generate EXECUTE commands, because new users would expect to see the result of the transformation right away in the Data Editor - and they probably don't have large datasets.

    --





  • 11.  RE: Efficiently creating a single variable from multiple binary variables

    Posted Tue December 15, 2020 08:52 AM
    Thank you Jon.  your words of wisdom will greatly affect my future workflow.

    I greatly appreciate your advice.

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