BookmarkSubscribeRSS Feed
xlwolfshirt
Calcite | Level 5

Hello, looking for some answers.  Already looked at the help page for array statements, but couldn't see anything related to my issue.

I'm examining the presence of an event over each month for the last two years, and have created a binary variable for each month ("1" if event was present, "0" if not).

Basically, whenever there is a "1" present, I need to know how many months it lasts.  For example, if month1, month2, and month3 all had 1's, I would need SAS to provide the number 3.  The tricky part is that there can be multiple groupings for each case.  For example, months 1 2 and 3 can all have "1", but months 19, 20, and 21 can also have "1"s.  So, I would need SAS to tell me there were two separate instances for one case where the event lasted 3 months.

I've tried using the following code someone else gave me to no avail:

data new; set old;

array months (24) month1-month24;

  do i=1 to 24 until (months(i) ne 0) ;

    if months(i)=0 then duration=i;

   end; run;


What this does is create a variable "i", which identifies the first month for a case that a "1" appears, and the variable "duration," which identifies the month where the last "0" appeared.  Any ideas on how to make this array do what I described ?  Thanks so much.

3 REPLIES 3
art297
Opal | Level 21

There is probably a more direct way, but the following (I think) accomplishes what you want to do:

data have;

  input id month1-month24;

  cards;

1 1 . 1 1 . 1 1 . . . 1 1 . . . 1 1 . 1 1 . 1 1 .

2 . . 1 1 1 . . . 1 1 . 1 1 1 1 . . . 1 1 . 1 1 1

3 . 1 1 1 1 1 1 . 1 1 1 1 1 1 1 . 1 1 1 1 1 1 . .

;

data want (drop=i counter durations:);

  set have;

  array months(*) month1-month24;

  array durations(12);

  call missing(of durations(*));

  if months(1) then do;

    counter=1;

    durations(counter)+1;

  end;

  else counter=0;

  do i=2 to dim(months);

    if months(i) then do;

      if missing(months(i-1)) then counter+1;

      durations(counter)+1;

    end;

  end;

  call sortn(of durations(*));

  i=11;

  max=durations(12);

  call missing(instances);

  if not missing(max) then do;

    instances=1;

    do while (durations(i) eq max);

      instances+1;

      i=i-1;

    end;

  end;

run;

Haikuo
Onyx | Level 15

Besides Art's answer, I think this thread may  be relevant to your questions:

https://communities.sas.com/thread/52032

If you find my part of answer interesting and need assistance to develop a solution for your very problem, I am here happy to help; I believe same goes with PG, who is much nicer than me Smiley Happy.

Haikuo

overmar
Obsidian | Level 7

I don't know if this is really all that more direct than Arthur's solution but it has fewer lines of code...

data havea;

    set have;

    array mn(24) month1-month24;

    array nm(24) _TEMPORARY_;

    array sb(24) _TEMPORARY_;

    array lg(24) _TEMPORARY_;

    do i = 1 to 24;

    nm(i) = 0;

    if mn(i) = 1 then sb(i) = 1;

    else sb(i) = 0;

    if mn(i) = 1 and i = 1 then nm(i) = 1;

    else if sb(i) = 1 then nm(i) = (sb(i)) + (nm(i-1));

    else nm(i) = 0;

    end;

    long = max(of nm

  • );
  •     do j = 1 to 24;

        if nm(j) = long then lg(j) = 1;

        else lg(j) = 0;

        times = sum(of lg

  • );
  •     end;

    run;

    SAS Innovate 2025: Save the Date

     SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

    Save the date!

    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
    • 3 replies
    • 969 views
    • 0 likes
    • 4 in conversation