Classic DoW loop flagging first variable

Accepted Solution Solved
Reply
Established User
Posts: 1
Accepted Solution

Classic DoW loop flagging first variable

Hi SASCommunity - I have a set of participants and a set of dates that are divided into active(active=1) and postactive(active=0) periods. I'd like to flag the first date of each active period for each partipant. 

For example, in the following dataset I would like to have a FLAG variable as follows (forgive me if syntaxical error here - I'm a NEWBY to SAS):

 

data participantperiods;

input participants dates active FLAG;

datalines;

1  date1  1  1

1  date2  1  0

1  date3  0  0

2  date4  1  1

2  date5  1  0

2  date6  1  0

2  date7  0  0

2  date8  0  0;

run;

 

I would like this to be in the form of a classic 2xDoW loop. This is what I have developed so far, but it is overflagging unfortunately:

 

data WANT;
     do until (last.PARTICIPANT);
          set HAVE;
          by PARTICIPANT;
          if first.PARTICIPANT then FIRSTFLAG=1;
     end;
     do until (last.PARTICIPANT);
          set HAVE;
          by PARTICIPANT;
     output;
     end;

     do until (last.PARTICIPANT);
          set HAVE;
          by PARTICIPANT;
          if FIRSTFLAG=1 and ACTIVE=1 then FIRSTACTIVEFLAG=1;
     end;

     do until (last.case_no);
          set Encountersbydayvendor4;
          by case_no;
          output;
     end;
run;

Accepted Solutions
Solution
‎08-18-2016 03:51 PM
Super User
Posts: 11,144

Re: Classic DoW loop flagging first variable

It appears as if the data is sorted by participants and descending active. If that is the case then this should work:

data want;
   set participantperiods;
   by participants descending active;
   if first.participants and first.active and active=1 then flag=1;
   else flag=0;
run;

if not then sort by participants descending active and date may get what you are looking for.

 

View solution in original post


All Replies
Solution
‎08-18-2016 03:51 PM
Super User
Posts: 11,144

Re: Classic DoW loop flagging first variable

It appears as if the data is sorted by participants and descending active. If that is the case then this should work:

data want;
   set participantperiods;
   by participants descending active;
   if first.participants and first.active and active=1 then flag=1;
   else flag=0;
run;

if not then sort by participants descending active and date may get what you are looking for.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 187 views
  • 2 likes
  • 2 in conversation