BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
brettmullga
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

2 REPLIES 2
Reeza
Super User

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;

brettmullga
Calcite | Level 5

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1207 views
  • 0 likes
  • 2 in conversation