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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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