Organize your data to have a threshold for each date for each patient id.
Sort by id date
read by id date
on first.id store save_date, clear flag
check the threshold on each record
if thrshold gt target
subtract date-save_date
if difference gt 90 set flag
store save_date
on last.id check the flag
I hope this helps Jim
Hi there,
Give this a try. I created some test data for you. I came up with two possible solutions. Enjoy.
hbi
/* not sure if your data actually looks like this; hopefully it's good enough to show these techniques */
/* create 1000 observations with 275 boolean variables */
DATA have;
LENGTH ID 8 today_minus1-today_minus275 3;
ARRAY num_array today_minus1-today_minus275;
DO ID=1 TO 1000;
DO OVER num_array;
num_array = floor(ranuni(1234)+0.94);
END;
output;
END;
RUN;
/* method 1: this is the easy way ... */
DATA want1(drop=today_minus1-today_minus275);
SET have;
LENGTH cat_275 $300 success_label $100
find_90_days end_90_days 8;
cat_275 = CATS(OF today_minus:);
find_90_days = INDEX(cat_275, REPEAT('1', 90));
IF find_90_days > 0 THEN DO;
end_90_days = 275 - INDEX(COMPRESS(REVERSE(cat_275)), REPEAT('1', 90)) + 1;
success_label = CAT("First occurrence of 90 consecutive days begins at position ", find_90_days,
" and ends at position ", end_90_days);
END;
RUN;
/* method 2: use arrays ... */
DATA want2(drop=array_loop consecutive_count);
SET have;
ARRAY num_array today_minus1-today_minus275;
LENGTH array_loop consecutive_count best_count 8 success_label $100;
array_loop = 0;
consecutive_count = 0;
best_count = 0;
DO OVER num_array;
array_loop + 1;
IF num_array = 1 THEN DO;
consecutive_count + 1;
IF consecutive_count >= best_count THEN best_count = consecutive_count;
IF consecutive_count >= 90 THEN success_label = CAT('First occurrence of 90 consecutive days detected at position: ', array_loop);
END;
ELSE consecutive_count = 0;
END;
Attaching a screenshot of the output window (method 1): ![]()
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.