SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ju_Fr
Calcite | Level 5

Hello all, 

 

I’m a new sas user and I’m stuck.

 

I have a patient dataset with one line per patient and a dummy variable for each of 12 days. 1 is for when the patient has medication on hand and 0 when they do not.

What I would like is to identify patients who have a medication gap of 4 days or more, along with the starting date of this gap.  

For example, patient A has such a gap that starts on day 4. Patient B has a gap of 2 days on days 2-3 but I’m only interested in their longer gap starting on day 8.

 

The only code I managed to come up with counted the total number of gap days per ID but that was inappropriate because I need to indentify patients who have a distinct gap >=4 days, and not patients who overall have missed medication more than 4 days over the period. 

 

Any insight would be warmly appreciated ! Thank you 

 

data have;

      input ID $ day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12;

      datalines ;

      a 1 1 1 0 0 0 0 0 0 0 0 1

      b 1 1 0 0 1 1 1 0 0 0 0 0

      c 1 0 1 1 1 1 1 1 0 0 0 0

      d 1 1 1 0 0 1 1 0 0 0 1 1     

run;

 

 

data want;

      input ID $ gap gap_date $;

      datalines ;

      a 1 day4

      b 1 day8

      c 1 day9

      d 0     

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

data want;
set have;

start = index(cats(of day1-day12), '0000');
drop day1-day12;
run;

@Ju_Fr wrote:

Hello all, 

 

I’m a new sas user and I’m stuck.

 

I have a patient dataset with one line per patient and a dummy variable for each of 12 days. 1 is for when the patient has medication on hand and 0 when they do not.

What I would like is to identify patients who have a medication gap of 4 days or more, along with the starting date of this gap.  

For example, patient A has such a gap that starts on day 4. Patient B has a gap of 2 days on days 2-3 but I’m only interested in their longer gap starting on day 8.

 

The only code I managed to come up with counted the total number of gap days per ID but that was inappropriate because I need to indentify patients who have a distinct gap >=4 days, and not patients who overall have missed medication more than 4 days over the period. 

 

Any insight would be warmly appreciated ! Thank you 

 

data have;

      input ID $ day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12;

      datalines ;

      a 1 1 1 0 0 0 0 0 0 0 0 1

      b 1 1 0 0 1 1 1 0 0 0 0 0

      c 1 0 1 1 1 1 1 1 0 0 0 0

      d 1 1 1 0 0 1 1 0 0 0 1 1     

run;

 

 

data want;

      input ID $ gap gap_date $;

      datalines ;

      a 1 day4

      b 1 day8

      c 1 day9

      d 0     

run;

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User

data want;
set have;

start = index(cats(of day1-day12), '0000');
drop day1-day12;
run;

@Ju_Fr wrote:

Hello all, 

 

I’m a new sas user and I’m stuck.

 

I have a patient dataset with one line per patient and a dummy variable for each of 12 days. 1 is for when the patient has medication on hand and 0 when they do not.

What I would like is to identify patients who have a medication gap of 4 days or more, along with the starting date of this gap.  

For example, patient A has such a gap that starts on day 4. Patient B has a gap of 2 days on days 2-3 but I’m only interested in their longer gap starting on day 8.

 

The only code I managed to come up with counted the total number of gap days per ID but that was inappropriate because I need to indentify patients who have a distinct gap >=4 days, and not patients who overall have missed medication more than 4 days over the period. 

 

Any insight would be warmly appreciated ! Thank you 

 

data have;

      input ID $ day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12;

      datalines ;

      a 1 1 1 0 0 0 0 0 0 0 0 1

      b 1 1 0 0 1 1 1 0 0 0 0 0

      c 1 0 1 1 1 1 1 1 0 0 0 0

      d 1 1 1 0 0 1 1 0 0 0 1 1     

run;

 

 

data want;

      input ID $ gap gap_date $;

      datalines ;

      a 1 day4

      b 1 day8

      c 1 day9

      d 0     

run;

 


 

Ju_Fr
Calcite | Level 5

Thank you @Reeza I did not know this function it works perfectly!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 518 views
  • 0 likes
  • 2 in conversation