Loop to create many variables from one variable.

Discussion in 'SPSS' started by Gesine Alders, Oct 19, 2011.

  1. Hello,

    I'm trying to create a type of attendance vector.
    My variables are: ID, and number of days attended in a row (i.e. 2,
    25, 42, etc.). I want to score forward from day_1 the number of days
    in a row participants attended. I've been able to create a vector, but
    have not been able to master the loop command that will assign a 1
    (attended) or a 0 (missed) to consecutive days starting from day_1.
    The assumption is that day_1 is the first day attended for each
    person.

    So, for example someone that attended 120 days, would have the score
    of "1" for each of the first 120 days in the vector.
    I have managed to create a vector:

    VECTOR day_(365F8.0).

    Would anyone with knowledge of LOOP commands be willing to help me out
    with this?

    Thank you,

    Gesine
     
    Gesine Alders, Oct 19, 2011
    #1
    1. Advertisements

  2. Gesine Alders

    Art Kendall Guest

    This example syntax does what I understand you to be asking for.

    Just curious. Why do you want to do this?

    data list list/id daysinarow (2f3).
    begin data
    1 2
    2 25
    3 42
    end data.
    vector day_(365f1).
    loop #day = 1 to 365.
    compute day_(#day) = range(#day,1,daysinarow).
    end loop.
    list.

    Art Kendall
    Social Research Consultants
     
    Art Kendall, Oct 19, 2011
    #2
    1. Advertisements

  3. Gesine Alders

    David Guest

    This can be simplified to:
    VECTOR day_(365f1).
    RECODE day_1 TO day_365 (else=0).
    LOOP #day = 1 to daysinarow.
    + COMPUTE day_(#day) = 1.
    END LOOP.

    OTOH:
    I tend to like my indexed variables to have the same length (for
    reasons of sorting etc)..
    NUMERIC day_001 TO day_365.
    RECODE day_001 TO day_365 (ELSE=0).
    VECTOR day_ = day_001 TO day_365.
    LOOP #day = 1 to daysinarow.
    + COMPUTE day_(#day) = 1.
    END LOOP.

    *HOWEVER**, I suspect we have *NOT* been told *EVERYTHING* of
    importance about the data ;-((
    What about people who attend 20 days, miss a day then attend the next
    40 days? Please provide more info about the actual structure of your
    data.
    HTH, David
     
    David, Oct 19, 2011
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.