Home > Enterprise >  In SPSS, Loop within a Do repeat
In SPSS, Loop within a Do repeat

Time:12-07

While doing data management in SPSS I´m trying to see if an individual was on sick leave on any given day, 1 to 365 days from an index date. Thus, if the individual is on sick leave on any of these dates i want a "1" in a variable corresponding to that day (v1-v365). I have tried different applications of the LOOP- and Do Repeat-command, but have to realize I don´t fully understand the logic behind it.

Data:

'''

data list list / ID(F2) Index(F11) Start_Sick(F11) End_Sick(F11).

BEGIN DATA.

1, 13808188800, 13770777600, 13839125500

2, 13837564800, 13705811200, 13839020000

3, 13807497600, 13847548800, 13855017600

4, 13841107200, 13839120000, 13855017600

5, 13830307200, 13847068800, 13855017600

end data.

dataset name MyData WINDOW=FRONT.

Formats Index Start_Sick End_Sick(date11).

VARIABLE LABELS Start_Sick 'Start of a sick leave period'.

VARIABLE LABELS End_Sick 'End of a sick leave period'.

VARIABLE LABELS Index 'Index date'.

''' My non working solution: '''

VECTOR v(365).

do repeat t = v1 to v365

 /v = v1 to v365.

LOOP cnt=1 TO 365 by 1.

IF Start_Sick LE datesum(Index,cnt,'days') and End_Sick GE datesum(Index,cnt,'days') t = 1.

END LOOP.

end repeat.

    EXECUTE.

''' When running i get either '1' in all of v1 to v365 for a case or all are missing. Am I using the wrong tools or am I using the tool wrongly?

Best wishes, Björn

CodePudding user response:

You only need the loop - the do repeat here is redundent. This is your own code, cleaned of the do repeat part and with a bit of adjustments - seems to work fine:

VECTOR v(365).
LOOP #cnt=1 TO 365 by 1.
IF Start_Sick LE datesum(Index,#cnt,'days') and End_Sick GE datesum(Index,#cnt,'days') v(#cnt) = 1.
END LOOP.
EXECUTE.
  • Related