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

How to select every other case in in SPSS data

  • 1.  How to select every other case in in SPSS data

    Posted 2 days ago

    Hello All,

    iO did not see an option to select every other case in in SPSS data. I am trying to split a dat file into two by selecting every other case. I do not want random selections. Thank you in advance.



    ------------------------------
    Dr. Frank
    Professor
    UMass
    ------------------------------


  • 2.  RE: How to select every other case in in SPSS data

    Posted 2 days ago
    Is it the file splitting or the numbering you're having the issue with?





  • 3.  RE: How to select every other case in in SPSS data

    Posted 2 days ago
    The obvious way to do this would be with a condition based on $casenum, but that doesn't work, because $casenum doesn't advance if a case is not selected.

    So do this.
    compute half = mod($casenum, 2) eq 1.
    filter by half.
    freq jobcat.

    change the condition to 0 for the other half.


    --





  • 4.  RE: How to select every other case in in SPSS data

    Posted 2 days ago

    Thank you Jon. I am splitting afile. What do you mean by "change the condition to 0 for the other half." I would like to get two different data files.



    ------------------------------
    Dr. Frank
    Professor
    UMass
    ------------------------------



  • 5.  RE: How to select every other case in in SPSS data

    Posted 12 hours ago

    When I read the initial post, I thought you wanted to apply the SPLIT FILE command.  But in your later post, you've said that you want to have two separate datasets, one with the odd-numbered rows and one with the even-numbered rows.  There may be an extension command to do that, or you could use XSAVE.  But here's another way you could try.  I'll illustrate using one of the datasets that comes with SPSS.

    NEW FILE. 
    DATASET CLOSE ALL.
    * Change path to point to folder with SPSS sample datasets.
    GET FILE = "C:\SPSSdata\survey_sample.sav".
    DATASET NAME original.
    COMPUTE OrigRow = $CASENUM.
    FORMATS OrigRow (F5.0).
    **************************************************.
    * Frank would like to get two different data files,
    * one with the odd rows, one with the even rows.
    **************************************************.
    * Make two copies of the original dataset.
    DATASET COPY Copy1.
    DATASET COPY Copy2.
    * Keep the odd-numbered rows in Copy1.
    DATASET ACTIVATE Copy1.
    SELECT IF MOD(OrigRow, 2) EQ 1.
    COMPUTE NewRow = $Casenum.
    FORMATS NewRow (F5.0).
    LIST OrigRow NewRow /CASES=FROM 1 to 10.
    DESCRIPTIVES OrigRow NewRow.
    * Keep the even-numbered rows in Copy1.
    DATASET ACTIVATE Copy2.
    SELECT IF MOD(OrigRow, 2) EQ 0.
    COMPUTE NewRow = $Casenum.
    FORMATS NewRow (F5.0).
    LIST OrigRow NewRow /CASES=FROM 1 to 10.

    Notice how the two SELECT IF commands differ.

    Here is the output from the two LIST commands.

    OrigRow NewRow 
     
         1       1 
         3       2 
         5       3 
         7       4 
         9       5 
        11       6 
        13       7 
        15       8 
        17       9 
        19      10 
     
    Number of cases read:  10    Number of cases listed:  10

    OrigRow NewRow 
     
         2       1 
         4       2 
         6       3 
         8       4 
        10       5 
        12       6 
        14       7 
        16       8 
        18       9 
        20      10 
     
    Number of cases read:  10    Number of cases listed:  10

    I hope this helps.



    ------------------------------
    Bruce Weaver
    ------------------------------



  • 6.  RE: How to select every other case in in SPSS data

    Posted 9 hours ago

    THANK YOU, I will try..



    ------------------------------
    Dr. Frank
    Professor
    UMass
    ------------------------------



  • 7.  RE: How to select every other case in in SPSS data

    Posted 2 days ago

    Just tried this. It deselected all of the cases! Idid not do anything with the condition...



    ------------------------------
    Dr. Frank
    Professor
    UMass
    ------------------------------



  • 8.  RE: How to select every other case in in SPSS data

    Posted 2 days ago
    The COMPUTE will generate a variable whose values alternate 0 (false) and 1 (true).  You can confirm that in the Data Editor.  Then you can use SELECT or Data > Select Cases to select just the 1 cases or just the 0 cases either in the active dataset or in a new one according to the choice in Select Cases.

    The actual selection will happen on the next data pass.  The data will be passed when you run a procedure or an EXECUTE command.






  • 9.  RE: How to select every other case in in SPSS data

    Posted 9 hours ago

    Thank you,

    I did the following in the first half:

    compute half = mod($casenum, 2) eq 1.
    filter by half.
    freq jobcat.

    I did the following in the second half, but all cases were deselected..
    compute half = mod($casenum, 2) eq 0.
    filter by half.
    freq jobcat.

    I must be missing something...



    ------------------------------
    Dr. Frank
    Professor
    UMass
    ------------------------------



  • 10.  RE: How to select every other case in in SPSS data

    Posted 9 hours ago

    I also added the EXECUTE command..



    ------------------------------
    Dr. Frank
    Professor
    UMass
    ------------------------------



  • 11.  RE: How to select every other case in in SPSS data

    Posted 8 hours ago

    After reading this most recent post, I've gone back to thinking that what you really want is SPLIT FILE, not two separate datasets.  If so, try something like this:

    NEW FILE. 
    DATASET CLOSE ALL.
    * Change path to point to folder with SPSS sample datasets.
    GET FILE = "C:\SPSSdata\survey_sample.sav".
    DATASET NAME original.
    COMPUTE Row = $CASENUM.
    COMPUTE Half = MOD($CASENUM, 2) EQ 1.
    RECODE Half (0=2).
    FORMATS Row (F5.0) / Half (F1). 
    LIST Row Half /CASES=FROM 1 to 10.
    SORT CASES BY Half.
    SPLIT FILE BY Half.
    FREQUENCIES Sex.
    SPLIT FILE OFF.

    The RECODE command is not really necessary, but some folks might find it easier to number the halves 1-2 rather than 0-1.  

    Here is the output from the LIST command:

    Row Half 
        1   1 
        2   2 
        3   1 
        4   2 
        5   1 
        6   2 
        7   1 
        8   2 
        9   1 
       10   2 
    Number of cases read:  10    Number of cases listed:  10

    And here is the output from FREQUENCIES:

    If you want two separate tables, you can add SEPARATE BY as an option for SPLIT FILE.  It's in the FM (i.e., the Fine Manual).  ;-)  



    ------------------------------
    Bruce Weaver
    ------------------------------