SPSS Statistics

 View Only
Expand all | Collapse all

Creating a variable ahead of restructuring the data

  • 1.  Creating a variable ahead of restructuring the data

    Posted Sun January 09, 2022 03:35 PM
    Hi. I have a dataset where participants have multiple rows of data and I need to restructure this so that each participant has one row of data.  I don't currently have a numerical variable that I can use as the index variable. I therefore want to create a variable that numbers each row of data that belongs to one participant . I found the $CASENUM function but can't work out how to make it apply that repeatedly for each participant based on the ID number variable. Does anyone have a solution for this?  Thanks.

    ------------------------------
    Phillippa Lally
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Creating a variable ahead of restructuring the data

    IBM Champion
    Posted Sun January 09, 2022 10:11 PM
    First, consider this restructuring example where an index exists in the data..
    data list list/id index x.
    begin data
    1 1 10
    1 2 12
    1 3 13
    2 1 21
    2 2 22
    3 2 32
    3 3 33
    end data
    cases to vars /id=id /index=index.

    As you can see, the index variable just counts sequentially inside each id.

    If you don't have an index like that you can construct one like this.
    data list list/id x.
    begin data
    1  10
    1  12
    1  13
    2  21
    2  22
    3  32
    3  33
    end data

    do if id ne lag(id) or $casenum eq 1..
    compute index = 1.
    else..
    compute index = lag(index) + 1.
    end if.

    This restarts the index values each time the id changes (with a special case for the first observation).

    If the cases are not already in id order, you will need to sort first.

    --