Search code examples
variablescountspss

How to create a variable that counts how many other cases has the same ID in SPSS


Trying to solve a puzzle here on SPSS.

I'm trying to find a way to count how many siblings each individual (or case) has, based on a variable named Mother_ID. It would basically count how many other cases has the same Mother_ID as the current record. Is there a way to do this?

Thanks!

Count within case doesn't work for this problem, as there are way too many different Mother_ID in the database.


Solution

  • AGGREGATE is your best friend here: https://www.ibm.com/docs/en/spss-statistics/29.0.0?topic=reference-aggregate

    AGGREGATE 
    /BREAK = Mother_ID
     /N_of_siblings = N(Mother_ID).
    

    This creates a new variable called N_of_siblings; for each record, it will show how many records there are sharing the same Mother_ID

    LE:

    Following eli-k's comments: the above code creats a new dataset, containing only Mother_ID and the N_of_siblings variables. If you want N_of_siblings added to your initial dataset, use /MODE = ADDVARIABLE:

    AGGREGATE 
    /MODE  = ADDVARIABLE
    /BREAK = Mother_ID
    /N_of_siblings = N(Mother_ID).