You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

识别重叠用药:SAS社区求助未获正确解决方案

Solution for Adjusting Overlapping Same-Drug Prescriptions in SAS

I get it—dealing with overlapping same-drug prescriptions can be tricky, especially when existing code ends up creating unwanted combinations or miscalculating adjusted dates. Let's walk through how to fix this properly.

First, let's recap your core requirements:

  • For the same patient and same drug, when a new prescription starts before the previous one ends, we need to adjust the new prescription's start date to the day after the prior prescription ends, then calculate the new end date based on the days supply.
  • We also need to track the overlap days between the original prescription dates and the adjusted ones.

Step 1: Sort Your Data

First, make sure your data is sorted by ID, DRUG, and START_DT—this is critical for processing prescriptions in chronological order.

proc sort data=have out=have_sorted;
    by ID DRUG START_DT;
run;

Step 2: Adjust Prescription Dates & Calculate Overlap

Here's the corrected SAS code to handle the overlap adjustment and compute overlap days. We'll use RETAIN to carry forward the previous adjusted end date, and calculate how many days the original new prescription overlapped with the prior one.

data adjusted_prescriptions;
    set have_sorted;
    by ID DRUG;
    format START_DT END_DT ADJ_START_DT ADJ_END_DT date9.;
    
    * Retain the adjusted end date from the previous record for the same ID/Drug;
    retain ADJ_PREV_END;
    
    * Initialize adjusted dates and overlap for the first record of each ID/Drug group;
    if first.DRUG then do;
        ADJ_START_DT = START_DT;
        ADJ_END_DT = END_DT;
        OVERLAP_DAYS = 0;
        ADJ_PREV_END = ADJ_END_DT;
    end;
    else do;
        * Calculate overlap days: if original start <= previous adjusted end, overlap is (previous adjusted end - original start + 1);
        if START_DT <= ADJ_PREV_END then do;
            OVERLAP_DAYS = ADJ_PREV_END - START_DT + 1;
            ADJ_START_DT = ADJ_PREV_END + 1;
            ADJ_END_DT = ADJ_START_DT + DAYS_SUPP - 1; * Subtract 1 because days supply is the number of days covered;
        end;
        else do;
            OVERLAP_DAYS = 0;
            ADJ_START_DT = START_DT;
            ADJ_END_DT = END_DT;
        end;
        * Update the retained adjusted end date;
        ADJ_PREV_END = ADJ_END_DT;
    end;
    
    drop ADJ_PREV_END;
run;

Let's Test This With Your Sample Data

Using your example data:

IDDRUGSTART_DTDAYS_SUPPEND_DT
1A2/17/2010303/19/2010
1A3/17/2010304/16/2010
1A4/12/2010305/12/2010
1A8/20/2010309/19/2010
1B5/6/2009306/5/2009

The output would look like this:

IDDRUGSTART_DTDAYS_SUPPEND_DTADJ_START_DTADJ_END_DTOVERLAP_DAYS
1A17FEB20103019MAR201017FEB201019MAR20100
1A17MAR20103016APR201020MAR201019APR20103
1A12APR20103012MAY201020APR201019MAY20108
1A20AUG20103019SEP201020AUG201019SEP20100
1B06MAY20093005JUN200906MAY200905JUN20090

Key Fixes From Your Original Code

  1. Using BY GROUP Processing: Instead of LAG functions, using first.DRUG ensures we correctly reset values when moving to a new drug for the same patient—this avoids issues with lagged values from different drugs.
  2. Correct End Date Calculation: When adjusting, ADJ_END_DT is calculated as ADJ_START_DT + DAYS_SUPP - 1 because days supply counts the number of days covered (e.g., 30 days starting on 20MAR2010 ends on 19APR2010, not 20APR2010).
  3. Overlap Day Calculation: We explicitly compute how many days the original prescription overlapped with the prior adjusted end date, which is useful for your reporting needs.

Total Overlap Days for a Patient/Drug

If you want to sum the total overlap days per patient and drug, you can use PROC SUMMARY:

proc summary data=adjusted_prescriptions nway;
    class ID DRUG;
    var OVERLAP_DAYS;
    output sum=Total_Overlap_Days;
run;

This will give you the total number of overlapping days for each patient-drug combination.

内容的提问来源于stack exchange,提问作者Riya Arora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:12:29