Counting the Length of Spells across month variables

Solved
Occasional Contributor
Posts: 6

Counting the Length of Spells across month variables

Data:

idmonth1month2month3month4month5month6
01010110

02

000110
03011010
04001110
05011110
06011110

Output:

# of spells: 8

Avg. Length: 2.375

Median: 2

Max: 4

Min: 1

Frequency Table

Freq#
12
23
31
42

The idea is that I want to treat this as survival data but I don't have a count of time. Thank you all in advance!

Accepted Solutions
Solution
‎08-08-2014 03:26 PM
Super User
Posts: 23,733

Re: Counting the Length of Spells across month variables

Change your data structure as a starting point:

data have;

input id \$2    month1    month2    month3    month4    month5    month6;

cards;

01    0    1    0    1    1    0

02    0    0    0    1    1    0

03    0    1    1    0    1    0

04    0    0    1    1    1    0

05    0    1    1    1    1    0

06    0    1    1    1    1    0

;

run;

data spells;

set have;

array months (6) month1-month6;

spell=0; duration=0;

do i=1 to 6;

if months(i)=1 then duration+1;

else if months(i)=0 and duration>0 then do;

spell+1;

output;

duration=0;

end;

end;

keep id spell duration;

run;

All Replies
Solution
‎08-08-2014 03:26 PM
Super User
Posts: 23,733

Re: Counting the Length of Spells across month variables

Change your data structure as a starting point:

data have;

input id \$2    month1    month2    month3    month4    month5    month6;

cards;

01    0    1    0    1    1    0

02    0    0    0    1    1    0

03    0    1    1    0    1    0

04    0    0    1    1    1    0

05    0    1    1    1    1    0

06    0    1    1    1    1    0

;

run;

data spells;

set have;

array months (6) month1-month6;

spell=0; duration=0;

do i=1 to 6;

if months(i)=1 then duration+1;

else if months(i)=0 and duration>0 then do;

spell+1;

output;

duration=0;

end;

end;

keep id spell duration;

run;

Occasional Contributor
Posts: 6

Re: Counting the Length of Spells across month variables

Thank you. I now better understand how to use arrays; they are a powerful tool!

🔒 This topic is solved and locked.