<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Help with Interrupted time series analysis: Code Optimization and Graphical Output in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968141#M48651</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Hello SAS experts,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I am working on single-group, two-stage interrupted time series analysis and have written SAS code for both the continuous outcome (using PROC MIXED) and the binary outcome (using a Poisson model). I have included detailed comments in the code and have accounted for potential confounders. I have also attached the resulting plots and output tables, as well as a sample of the original dataset for reference.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Could you please review the PROC MIXED version and the Poisson version of the code, along with the generated graphs and results, and let me know if they are correct? I would greatly appreciate any suggestions for optimization.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you very much for your help!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*********************************************
Step 1: Single-group, two-stage ITS (continuous Y) — PROC MIXED version
   variables:
     - id             : Subject identifier
     - MO             : Month (1–12)
     - time 1           : (equal to MO)
     - Y              : Continuous outcome variable (each person’s annual observation)
     - intervention1  : Indicator for first intervention (0/1), equals 1 from MO=4 onward
     - intervention2  : Indicator for second intervention (0/1), equals 1 from MO=6 onward
     - city           : Binary indicator for location (0/1)
     - count_gr       : Outpatient visit count group (1/2/3)
*********************************************/

proc mixed data=ITS.COST method=ml;
  /* (1) Specify categorical variables */
  class id city count_gr;
  /* (2) Main model: time trend, two intervention effects and interactions, and covariates */
  model COST = /* change variable placeholder */
        time1
        intervention1 time1*intervention1
        intervention2 time1*intervention2
        /* Covariates */
        city count_gr
        / solution;
  /* (3) Specify repeated measures structure (multiple yearly observations per subject): unstructured covariance */
  repeated / subject=id type=un;

  /* (4) Save model results to its_model_continuous for subsequent PROC PLM scoring */
  store out=its_model_continuous;

  /* (5) Estimate statements: segmented regression estimates of slopes and level changes */
  estimate 'Pre slope' time1 1 / cl e;
  estimate 'Post1 slope' time1 1 time1*intervention1 1 / cl e;
  estimate 'Gap1 (1st level shift)' intervention1 1 time1*intervention1 1 / cl e;
  estimate 'Post2 slope' time1 1 time1*intervention1 1 time1*intervention2 1 / cl e;
  estimate 'Gap2 (2nd level shift)' intervention2 1 time1*intervention2 1 / cl e;
run;

/*********************************************
Step 2: Use PROC PLM to obtain predicted values and standard errors for each time point
*********************************************/
proc plm restore=its_model_continuous;
  score data=ITS.COST out=pred_panel_cont  
        predicted=pred_value
        stderr=StdErrPred;
run;

/*********************************************
Step 3: Aggregate average predicted values and 95% confidence intervals for each time point
*********************************************/
proc summary data=pred_panel_cont nway;
  class time1;
  var pred_value StdErrPred;
  output out=avg_pred_cont mean= / autoname;
run;

data avg_pred_cont;
  set avg_pred_cont;
  /* If after summary, variable names are pred_value_Mean and StdErrPred_Mean */
  pred = pred_value_Mean;
  se   = StdErrPred_Mean;
  lcl  = pred - 1.96 * se;
  ucl  = pred + 1.96 * se;
run;
/*********************************************
Step 4: Plot ITS predicted values (continuous Y → predicted values and 95% CI)
*********************************************/
ods graphics on / attrpriority=none;
proc sgplot data=avg_pred_cont;
  /* Confidence interval band */
  band x=time1 lower=lcl upper=ucl
       / fillattrs=(color=gray transparency=0.9)
         legendlabel="95% Confidence Interval";
  /* Predicted values line */
  series x=time1 y=pred
         / markers
           lineattrs=(thickness=2 color=blue)
           markerattrs=(symbol=circlefilled size=8 color=blue)
           legendlabel="Predicted Value";
  /* Add intervention point reference lines (first intervention time1=3, second intervention time1=5) */
  refline 3 / axis=x lineattrs=(pattern=shortdash color=black)
               label="First Intervention" labelattrs=(weight=bold size=9);
  refline 5 / axis=x lineattrs=(pattern=shortdash color=black)
               label="Second Intervention" labelattrs=(weight=bold size=9);
  xaxis label="Time (Month)"
        values=(1 to 12 by 1)   /* Force display of 1,2,...,12 */
        valuesdisplay=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12")
        integer;   /* Display only integer tick values */
  yaxis label="Total Cost";
  keylegend / position=bottom location=outside across=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;ITS.COST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_0-1749038623241.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107590i9E784F143FA25086/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_0-1749038623241.png" alt="tina_ting_0-1749038623241.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_1-1749038629931.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107591iFF1C3796A0965600/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_1-1749038629931.png" alt="tina_ting_1-1749038629931.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_2-1749038632207.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107592i997AD853E70288A5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_2-1749038632207.png" alt="tina_ting_2-1749038632207.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_3-1749038634711.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107593i295C171B9F2C0187/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_3-1749038634711.png" alt="tina_ting_3-1749038634711.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_4-1749038642910.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107594i150FC537B65C4CDA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_4-1749038642910.png" alt="tina_ting_4-1749038642910.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*********************************************
Single-group two-stage ITS (binary Y) — Poisson model version
*********************************************/

/* Step 1: Aggregate → annual PIM_count, N_person */
proc sql;
  create table ITS.POLY_agg as
  select MO,
    /* Cumulative number of POLY events */
    sum(POLY) as POLY_count, 
    /* Total number of people in that year (distinct id) */
    count(distinct id) as N_person,
    /* 1. For binary categorical variables, use mean() to get proportion (0/1 only needs one variable) */
    mean(city)                         as prop_city,       /* Proportion of urban vs. non-urban */
    /* 4. Proportions for count_gr (outpatient visit count groups 1, 2, 3) */
    sum(case when count_gr = 1 then 1 else 0 end) / count(distinct id)    as prop_count1,
    sum(case when count_gr = 2 then 1 else 0 end) / count(distinct id)    as prop_count2,
    sum(case when count_gr = 3 then 1 else 0 end) / count(distinct id)    as prop_count3
  from VAR.POLY
  group by MO
  order by MO
  ;
quit;

/* Step 2: Add time, intervention indicators, and offset */
data ITS.POLY_poisson;
  set ITS.POLY_agg;
  time = MO;                    
  if MO &amp;gt;= 4 then intervention1 = 1;  else intervention1 = 0;
  if MO &amp;gt;= 6 then intervention2 = 1;  else intervention2 = 0;
  lnN = log(N_person);                     /* Poisson offset */
run;

/* Step 3: ITS model estimation */
proc genmod data=ITS.POLY_poisson;
  model POLY_count =
        time 
        intervention1 time*intervention1
        intervention2 time*intervention2
        /* Control covariates: binary categorical variables (0/1 feed in as proportions) */
        prop_city            /* Proportion of urban vs. non-urban (city=1) */
        /* Use prop_count1, prop_count2; count_gr=0 is reference */
        prop_count1          /* Proportion with count_gr=1 */
        prop_count2          /* Proportion with count_gr=2 */
        / dist=poisson link=log offset=lnN;

  /* ITS estimates for slopes/level changes in each stage (adjust as needed) */
  estimate 'PRE slope'                 time 1 / exp;
  estimate 'POST1 slope'              time 1 time*intervention1 1 / exp;
  estimate 'GAP1 (1st level shift)'   intervention1 1 / exp;
  estimate 'POST2 slope'              time 1 time*intervention1 1 time*intervention2 1 / exp;
  estimate 'GAP2 (2nd level shift)'   intervention2 1 / exp;
  output out=pred1 pred=predcount lower=lcl upper=ucl;
run;

/* Step 4a: Calculate “rate” in pred1, If pred1 already contains POLY_count and N_person, process as follows:*/
data pred1_rate;
  set pred1;
  /* Calculate rate = event count ÷ population */
  rate = POLY_count / N_person;
  format rate percent7.2;
run;
/* Step 4b: Plot “rate” line and scatter with SGPLOT */
proc sgplot data=pred1_rate noautolegend;
  /* (1) Rate line */
  series x=time y=rate
         / lineattrs=(thickness=2 color=blue)
           name="RateLine";
  /* (2) Rate scatter points + data labels */
  scatter x=time y=rate
          / markerattrs=(symbol=CircleFilled color=blue size=8)
            datalabel=rate
            datalabelattrs=(size=9 color=black)
            name="RatePts";
  /* (3) Intervention time points (first at time=4; second at time=6) */
  refline 4 6 / axis=x  lineattrs=(pattern=shortdash color=black);
  /* (4) Axis settings */
  xaxis label="Time (Year)"
        values=(1 to 12 by 1)
        valuesdisplay=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12")
        integer;
  yaxis label="POLY Rate";
  /* (5) Legend */
  keylegend "RatePts"  / title="Observed Rate (Points/Labels)" position=topright across=1;
  keylegend "RateLine" / title="Observed Rate (Line)"         position=topright across=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;VAR.POLY&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_5-1749038683885.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107595iB545F927797DD28B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_5-1749038683885.png" alt="tina_ting_5-1749038683885.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;ITS.POLY_agg&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_6-1749038694752.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107596i6E97768B0D49C848/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_6-1749038694752.png" alt="tina_ting_6-1749038694752.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;ITS.POLY_poisson&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_7-1749038708391.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107597iA2941E6BDF0DA83A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_7-1749038708391.png" alt="tina_ting_7-1749038708391.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_8-1749038714835.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107598iCA9DF18CF7C41195/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_8-1749038714835.png" alt="tina_ting_8-1749038714835.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_9-1749038720529.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107599i84F9121A879D3DDF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_9-1749038720529.png" alt="tina_ting_9-1749038720529.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_10-1749038727266.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107600iC2C0A456B974CD71/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_10-1749038727266.png" alt="tina_ting_10-1749038727266.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_11-1749038733258.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107601i849238AE095840FF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_11-1749038733258.png" alt="tina_ting_11-1749038733258.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 04 Jun 2025 12:07:36 GMT</pubDate>
    <dc:creator>tina_ting</dc:creator>
    <dc:date>2025-06-04T12:07:36Z</dc:date>
    <item>
      <title>Help with Interrupted time series analysis: Code Optimization and Graphical Output</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968141#M48651</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello SAS experts,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I am working on single-group, two-stage interrupted time series analysis and have written SAS code for both the continuous outcome (using PROC MIXED) and the binary outcome (using a Poisson model). I have included detailed comments in the code and have accounted for potential confounders. I have also attached the resulting plots and output tables, as well as a sample of the original dataset for reference.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Could you please review the PROC MIXED version and the Poisson version of the code, along with the generated graphs and results, and let me know if they are correct? I would greatly appreciate any suggestions for optimization.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you very much for your help!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*********************************************
Step 1: Single-group, two-stage ITS (continuous Y) — PROC MIXED version
   variables:
     - id             : Subject identifier
     - MO             : Month (1–12)
     - time 1           : (equal to MO)
     - Y              : Continuous outcome variable (each person’s annual observation)
     - intervention1  : Indicator for first intervention (0/1), equals 1 from MO=4 onward
     - intervention2  : Indicator for second intervention (0/1), equals 1 from MO=6 onward
     - city           : Binary indicator for location (0/1)
     - count_gr       : Outpatient visit count group (1/2/3)
*********************************************/

proc mixed data=ITS.COST method=ml;
  /* (1) Specify categorical variables */
  class id city count_gr;
  /* (2) Main model: time trend, two intervention effects and interactions, and covariates */
  model COST = /* change variable placeholder */
        time1
        intervention1 time1*intervention1
        intervention2 time1*intervention2
        /* Covariates */
        city count_gr
        / solution;
  /* (3) Specify repeated measures structure (multiple yearly observations per subject): unstructured covariance */
  repeated / subject=id type=un;

  /* (4) Save model results to its_model_continuous for subsequent PROC PLM scoring */
  store out=its_model_continuous;

  /* (5) Estimate statements: segmented regression estimates of slopes and level changes */
  estimate 'Pre slope' time1 1 / cl e;
  estimate 'Post1 slope' time1 1 time1*intervention1 1 / cl e;
  estimate 'Gap1 (1st level shift)' intervention1 1 time1*intervention1 1 / cl e;
  estimate 'Post2 slope' time1 1 time1*intervention1 1 time1*intervention2 1 / cl e;
  estimate 'Gap2 (2nd level shift)' intervention2 1 time1*intervention2 1 / cl e;
run;

/*********************************************
Step 2: Use PROC PLM to obtain predicted values and standard errors for each time point
*********************************************/
proc plm restore=its_model_continuous;
  score data=ITS.COST out=pred_panel_cont  
        predicted=pred_value
        stderr=StdErrPred;
run;

/*********************************************
Step 3: Aggregate average predicted values and 95% confidence intervals for each time point
*********************************************/
proc summary data=pred_panel_cont nway;
  class time1;
  var pred_value StdErrPred;
  output out=avg_pred_cont mean= / autoname;
run;

data avg_pred_cont;
  set avg_pred_cont;
  /* If after summary, variable names are pred_value_Mean and StdErrPred_Mean */
  pred = pred_value_Mean;
  se   = StdErrPred_Mean;
  lcl  = pred - 1.96 * se;
  ucl  = pred + 1.96 * se;
run;
/*********************************************
Step 4: Plot ITS predicted values (continuous Y → predicted values and 95% CI)
*********************************************/
ods graphics on / attrpriority=none;
proc sgplot data=avg_pred_cont;
  /* Confidence interval band */
  band x=time1 lower=lcl upper=ucl
       / fillattrs=(color=gray transparency=0.9)
         legendlabel="95% Confidence Interval";
  /* Predicted values line */
  series x=time1 y=pred
         / markers
           lineattrs=(thickness=2 color=blue)
           markerattrs=(symbol=circlefilled size=8 color=blue)
           legendlabel="Predicted Value";
  /* Add intervention point reference lines (first intervention time1=3, second intervention time1=5) */
  refline 3 / axis=x lineattrs=(pattern=shortdash color=black)
               label="First Intervention" labelattrs=(weight=bold size=9);
  refline 5 / axis=x lineattrs=(pattern=shortdash color=black)
               label="Second Intervention" labelattrs=(weight=bold size=9);
  xaxis label="Time (Month)"
        values=(1 to 12 by 1)   /* Force display of 1,2,...,12 */
        valuesdisplay=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12")
        integer;   /* Display only integer tick values */
  yaxis label="Total Cost";
  keylegend / position=bottom location=outside across=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;ITS.COST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_0-1749038623241.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107590i9E784F143FA25086/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_0-1749038623241.png" alt="tina_ting_0-1749038623241.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_1-1749038629931.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107591iFF1C3796A0965600/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_1-1749038629931.png" alt="tina_ting_1-1749038629931.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_2-1749038632207.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107592i997AD853E70288A5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_2-1749038632207.png" alt="tina_ting_2-1749038632207.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_3-1749038634711.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107593i295C171B9F2C0187/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_3-1749038634711.png" alt="tina_ting_3-1749038634711.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_4-1749038642910.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107594i150FC537B65C4CDA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_4-1749038642910.png" alt="tina_ting_4-1749038642910.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*********************************************
Single-group two-stage ITS (binary Y) — Poisson model version
*********************************************/

/* Step 1: Aggregate → annual PIM_count, N_person */
proc sql;
  create table ITS.POLY_agg as
  select MO,
    /* Cumulative number of POLY events */
    sum(POLY) as POLY_count, 
    /* Total number of people in that year (distinct id) */
    count(distinct id) as N_person,
    /* 1. For binary categorical variables, use mean() to get proportion (0/1 only needs one variable) */
    mean(city)                         as prop_city,       /* Proportion of urban vs. non-urban */
    /* 4. Proportions for count_gr (outpatient visit count groups 1, 2, 3) */
    sum(case when count_gr = 1 then 1 else 0 end) / count(distinct id)    as prop_count1,
    sum(case when count_gr = 2 then 1 else 0 end) / count(distinct id)    as prop_count2,
    sum(case when count_gr = 3 then 1 else 0 end) / count(distinct id)    as prop_count3
  from VAR.POLY
  group by MO
  order by MO
  ;
quit;

/* Step 2: Add time, intervention indicators, and offset */
data ITS.POLY_poisson;
  set ITS.POLY_agg;
  time = MO;                    
  if MO &amp;gt;= 4 then intervention1 = 1;  else intervention1 = 0;
  if MO &amp;gt;= 6 then intervention2 = 1;  else intervention2 = 0;
  lnN = log(N_person);                     /* Poisson offset */
run;

/* Step 3: ITS model estimation */
proc genmod data=ITS.POLY_poisson;
  model POLY_count =
        time 
        intervention1 time*intervention1
        intervention2 time*intervention2
        /* Control covariates: binary categorical variables (0/1 feed in as proportions) */
        prop_city            /* Proportion of urban vs. non-urban (city=1) */
        /* Use prop_count1, prop_count2; count_gr=0 is reference */
        prop_count1          /* Proportion with count_gr=1 */
        prop_count2          /* Proportion with count_gr=2 */
        / dist=poisson link=log offset=lnN;

  /* ITS estimates for slopes/level changes in each stage (adjust as needed) */
  estimate 'PRE slope'                 time 1 / exp;
  estimate 'POST1 slope'              time 1 time*intervention1 1 / exp;
  estimate 'GAP1 (1st level shift)'   intervention1 1 / exp;
  estimate 'POST2 slope'              time 1 time*intervention1 1 time*intervention2 1 / exp;
  estimate 'GAP2 (2nd level shift)'   intervention2 1 / exp;
  output out=pred1 pred=predcount lower=lcl upper=ucl;
run;

/* Step 4a: Calculate “rate” in pred1, If pred1 already contains POLY_count and N_person, process as follows:*/
data pred1_rate;
  set pred1;
  /* Calculate rate = event count ÷ population */
  rate = POLY_count / N_person;
  format rate percent7.2;
run;
/* Step 4b: Plot “rate” line and scatter with SGPLOT */
proc sgplot data=pred1_rate noautolegend;
  /* (1) Rate line */
  series x=time y=rate
         / lineattrs=(thickness=2 color=blue)
           name="RateLine";
  /* (2) Rate scatter points + data labels */
  scatter x=time y=rate
          / markerattrs=(symbol=CircleFilled color=blue size=8)
            datalabel=rate
            datalabelattrs=(size=9 color=black)
            name="RatePts";
  /* (3) Intervention time points (first at time=4; second at time=6) */
  refline 4 6 / axis=x  lineattrs=(pattern=shortdash color=black);
  /* (4) Axis settings */
  xaxis label="Time (Year)"
        values=(1 to 12 by 1)
        valuesdisplay=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12")
        integer;
  yaxis label="POLY Rate";
  /* (5) Legend */
  keylegend "RatePts"  / title="Observed Rate (Points/Labels)" position=topright across=1;
  keylegend "RateLine" / title="Observed Rate (Line)"         position=topright across=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;VAR.POLY&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_5-1749038683885.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107595iB545F927797DD28B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_5-1749038683885.png" alt="tina_ting_5-1749038683885.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;ITS.POLY_agg&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_6-1749038694752.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107596i6E97768B0D49C848/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_6-1749038694752.png" alt="tina_ting_6-1749038694752.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Dataset&lt;/SPAN&gt;：&lt;SPAN&gt;ITS.POLY_poisson&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_7-1749038708391.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107597iA2941E6BDF0DA83A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_7-1749038708391.png" alt="tina_ting_7-1749038708391.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_8-1749038714835.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107598iCA9DF18CF7C41195/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_8-1749038714835.png" alt="tina_ting_8-1749038714835.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_9-1749038720529.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107599i84F9121A879D3DDF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_9-1749038720529.png" alt="tina_ting_9-1749038720529.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_10-1749038727266.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107600iC2C0A456B974CD71/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_10-1749038727266.png" alt="tina_ting_10-1749038727266.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tina_ting_11-1749038733258.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107601i849238AE095840FF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tina_ting_11-1749038733258.png" alt="tina_ting_11-1749038733258.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jun 2025 12:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968141#M48651</guid>
      <dc:creator>tina_ting</dc:creator>
      <dc:date>2025-06-04T12:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interrupted time series analysis: Code Optimization and Graphical Output</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968156#M48652</link>
      <description>&lt;P&gt;You might want to look over &lt;A href="http://support.sas.com/kb/70498" target="_self"&gt;this note&lt;/A&gt; on ITS.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jun 2025 15:05:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968156#M48652</guid>
      <dc:creator>StatDave</dc:creator>
      <dc:date>2025-06-04T15:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Interrupted time series analysis: Code Optimization and Graphical Output</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968385#M48688</link>
      <description>&lt;P&gt;Thank you for providing the information.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;If I want to take the ITS code for a two-group, one-stage design with a continuous response and modify it to a two-group, two-stage design, how should I change it? If I also want to include covariates (categorical variables), where in the syntax should I place those variables?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*two-group, one-stage, continuous response*/&lt;BR /&gt;data ITS.COST;
  set VAR.COST;
  if MO &amp;gt;= 4 then iv1 = 1;  else iv1 = 0; *iv1:在第3個月介入;
  if MO &amp;gt;= 6 then iv2 = 1;  else iv2 = 0; *iv2:在第5個月介入;
  month=MO;
  y=cost;
  expiv = catx('_', put(MCC, 1.), put(iv1, 1.));
  ph = catx('_', put(MCC, 1.), put(iv1, 1.), put(iv2, 1.));
run;

proc gee data=ITS.COST; 
        class id expiv;
        model y = expiv expiv*month / noint;
        repeated subject=id;
        effectplot slicefit(x=month sliceby=expiv) / extend=data clm;
        estimate 
         'compare group slopes, pre'        expiv*month -1  0  1 0,
         'compare group slopes, post'       expiv*month  0 -1  0 1,
         'compare period slopes, unexposed' expiv*month -1  1  0 0,
         'compare period slopes, exposed'   expiv*month  0  0 -1 1,
         'compare post-pre slope changes'   expiv*month  1 -1 -1 1,
         '1 month change unexposed' expiv -1  1  0 0  expiv*month -3  4  0 0,
         '1 month change exposed'   expiv  0  0 -1 1  expiv*month  0  0 -3 4,
         '1 month change diff'      expiv  1 -1 -1 1  expiv*month  3 -4 -3 4;
        run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Likewise, for the ITS code for a two-group, one-stage design with a binary response, how would I convert it to a two-group, two-stage design and add covariates?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*two-group, one-stage, binary response*/&lt;BR /&gt;data ITS.poly;&lt;BR /&gt;set VAR.poly;&lt;BR /&gt;if MO &amp;gt;= 4 then iv1 = 1; else iv1 = 0; *iv1:在第3個月介入;&lt;BR /&gt;if MO &amp;gt;= 6 then iv2 = 1; else iv2 = 0; *iv2:在第5個月介入;&lt;BR /&gt;if MO&amp;lt;=3 then MO1 = MO; else if MO&amp;gt;=4 then MO1=MO-3;&lt;BR /&gt;month=MO;&lt;BR /&gt;y=poly;&lt;BR /&gt;expiv = catx('_', put(MCC, 1.), put(iv1, 1.));&lt;BR /&gt;ph = catx('_', put(MCC, 1.), put(iv1, 1.), put(iv2, 1.));&lt;BR /&gt;run;&lt;BR /&gt;
proc gee data= ITS.poly; 
        class id expiv;
        model y(event="1") = expiv month expiv*month / dist=bin noint;
        repeated subject=id;
        run;
proc gee data=ITS.poly; 
        class id expiv;
        model y(event="1") = expiv expiv*month / dist=bin noint;
        repeated subject=id;
        effectplot fit(x=month plotby(pack)=expiv) / clm;
        effectplot fit(x=month plotby(pack)=expiv) / link clm;
        estimate 'm3 log odds' expiv 1 0 expiv*month 3 0 / ilink;
        estimate 'm4 log odds' expiv 0 1 expiv*month 0 1 / ilink;
        estimate 'm3 to m4 odds ratio' expiv -1 1 expiv*month -3 1 / exp cl;
        store gee;
        run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Jun 2025 07:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Help-with-Interrupted-time-series-analysis-Code-Optimization-and/m-p/968385#M48688</guid>
      <dc:creator>tina_ting</dc:creator>
      <dc:date>2025-06-08T07:07:17Z</dc:date>
    </item>
  </channel>
</rss>

