Help using Base SAS procedures

Please, please help! Trying to use an array statement to identify duration of an event, can't find what's wrong.

Reply
N/A
Posts: 1

Please, please help! Trying to use an array statement to identify duration of an event, can't find what's wrong.

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.

PROC Star
Posts: 7,356

Re: Please, please help! Trying to use an array statement to identify duration of an event, can't find what's wrong.

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 durationsSmiley Happy;

  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;

Respected Advisor
Posts: 3,124

Re: Please, please help! Trying to use an array statement to identify duration of an event, can't find what's wrong.

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

Frequent Contributor
Posts: 83

Re: Please, please help! Trying to use an array statement to identify duration of an event, can't find what's wrong.

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;

    Ask a Question
    Discussion stats
    • 3 replies
    • 208 views
    • 0 likes
    • 4 in conversation