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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

1 REPLY 1
ballardw
Super User

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.

 

sas-innovate-wordmark-gradient-background 3.pngSAS Innovate

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 1471 views
  • 2 likes
  • 2 in conversation