I am working on a study to evaluate the association of a policy change with the proportion of new drug users over time. I would like to create a graph based on my autoregression data (interrupted time-series analysis). Building the graph was successful, but I have problems with labelling the x axis.
The dataset is structured as follows (sample data): see attachment (excel)
I applied following comands:
data graph_proportion; set autoreg_out;
if added=0 then do;
if 1 <= period <= 48 then modeled1=pred;
if 1 <= period <= 48 then band1_l=pred_l;
if 1 <= period <= 48 then band1_u=pred_u;
if 49 <= period <= 63 then modeled2=pred;
if 49 <= period <= 63 then band2_l=pred_l;
if 49 <= period <= 63 then band2_u=pred_u;
end;
if added=1 then do;
projection=pred;
end;
label prop_newusers='Observed rate'
modeled1='Modeled trend'
projection="Predicted trend";
run;
ods pdf style=HTMLBlue file="proportion" style=HTMLBlue image_dpi=600
path='Path xy';
ods graphics / reset width=450px height=350 px imagename="fig1a" imagefmt=PDF;
proc sgplot data= graph_proportion noautolegend;
band x=period upper=band1_u lower=band1_l / noextend;
band x=period upper=band2_u lower=band2_l / noextend;
scatter x=period y=proportion / markerattrs=(symbol=circlefilled color=CX203554 size=4) name="Observed rate";
series x=period y=modeled1 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="Modeled trend";
series x=period y=modeled2 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="none";
series x=period y=projection / lineattrs=(pattern=shortdash color=/*CX536582*/black thickness=2) name="Predicted trend";
refline 2006.5 / axis=x lineattrs=(pattern=dash thickness=2 color=grey);
keylegend "Observed proportion" "Modeled trend" "Predicted trend" / location=inside position=topright across=1;
yaxis label="Average proportion per 10000 patient" values=(0 to 20 by 1) labelattrs=(size=10) valueattrs=(size=8);
xaxis fitpolicy=ROTATETHIN /*display=(noticks)*/ values=(1 to 63 by 1) valuesdisplay=('1Jan2015' '1Jan2016' '1Jan2017' '1Jan2018' '1Jan2019' '1Jan2020' '3Mar2020') labelattrs=(size=10) valueattrs=(size=8) display=(nolabel);
run;
I have 63 time-periods in total, each time-period represents a 30-day period between 1 January 2015 and 3 March 2020. So far I have only figured out how to label the x-axis at regular intervals. However, I want to label specific data points / time periods only, e.g. I need to specify to label period 1 as 1Jan2015, and period 63 as 3Mar2020, as well as some data points in between.
Thank you for your help!
In general, it is better to use SAS dates than to define your own custom periods. The axes in the SG procedures know how to place tick marks on time axes and the REFLINE statement knows how to add reference lines at the tick marks. Therefore, I suggest you plot the data versus a date variable and add reference lines at the special dates that you want to emphasize.
The following example uses the simple formula "days since 01Jan2015' to compute a date from the time period. However, if you have the original dates, you might want to use them
data Want;
set graph_proportion ;
/* "each time-period represents a 30-day period between 1 January 2015... */
Date = '01Jan2015'd + 30*period;
format Date DATE7.;
run;
proc sgplot data=Want noautolegend;
band x=Date upper=band1_u lower=band1_l / noextend;
band x=Date upper=band2_u lower=band2_l / noextend;
scatter x=Date y=proportion / markerattrs=(symbol=circlefilled color=CX203554 size=4) name="Observed rate";
series x=Date y=modeled1 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="Modeled trend";
series x=Date y=modeled2 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="none";
series x=Date y=projection / lineattrs=(pattern=shortdash color=/*CX536582*/black thickness=2) name="Predicted trend";
yaxis label="Average proportion per 10000 patient" values=(0 to 20 by 1) labelattrs=(size=10) valueattrs=(size=8);
refline '1Jan2015'd '1Jan2016'd '1Jan2017'd '1Jan2018'd '1Jan2019'd '1Jan2020'd / axis=x;
refline '3Mar2020'd / axis=x label='03Mar20';
keylegend "Observed proportion" "Modeled trend" "Predicted trend" / location=inside position=topleft across=1 opaque;
run;
In general, it is better to use SAS dates than to define your own custom periods. The axes in the SG procedures know how to place tick marks on time axes and the REFLINE statement knows how to add reference lines at the tick marks. Therefore, I suggest you plot the data versus a date variable and add reference lines at the special dates that you want to emphasize.
The following example uses the simple formula "days since 01Jan2015' to compute a date from the time period. However, if you have the original dates, you might want to use them
data Want;
set graph_proportion ;
/* "each time-period represents a 30-day period between 1 January 2015... */
Date = '01Jan2015'd + 30*period;
format Date DATE7.;
run;
proc sgplot data=Want noautolegend;
band x=Date upper=band1_u lower=band1_l / noextend;
band x=Date upper=band2_u lower=band2_l / noextend;
scatter x=Date y=proportion / markerattrs=(symbol=circlefilled color=CX203554 size=4) name="Observed rate";
series x=Date y=modeled1 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="Modeled trend";
series x=Date y=modeled2 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="none";
series x=Date y=projection / lineattrs=(pattern=shortdash color=/*CX536582*/black thickness=2) name="Predicted trend";
yaxis label="Average proportion per 10000 patient" values=(0 to 20 by 1) labelattrs=(size=10) valueattrs=(size=8);
refline '1Jan2015'd '1Jan2016'd '1Jan2017'd '1Jan2018'd '1Jan2019'd '1Jan2020'd / axis=x;
refline '3Mar2020'd / axis=x label='03Mar20';
keylegend "Observed proportion" "Modeled trend" "Predicted trend" / location=inside position=topleft across=1 opaque;
run;
I have a follow-up question:
Why does the graph end at July 2020? I would like it to end where the data / courve ends. How can I implement this?
All in all, I would like to have the x-axis labeled as indicated in the refline, but end it at the end of the data collection (3Mar2020).
Thank you for your help again!
In the PROC SGPLOT call, try adding
xaxis max='03Mar2020'd;
You might also have to experiment with the OFFSETMAX= option to get it to show the value for the reference line. For example, try OFFSETMAX=0.05 and then increase or decrease that value until it looks the way you want it to look.
Unfortunately, it does not change anything on the graph, when I apply following commant:
proc sgplot data=Want noautolegend;
band x=Date upper=band1_u lower=band1_l / noextend;
band x=Date upper=band2_u lower=band2_l / noextend;
scatter x=Date y=proportion / markerattrs=(symbol=circlefilled color=CX203554 size=4) name="Observed rate";
series x=Date y=modeled1 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="Modeled trend";
series x=Date y=modeled2 / lineattrs=(pattern=solid color=CX536582 thickness=2) name="none";
series x=Date y=projection / lineattrs=(pattern=shortdash color=/*CX536582*/black thickness=2) name="Predicted trend";
yaxis label="Average proportion per 10000 patient" values=(0 to 20 by 1) labelattrs=(size=10) valueattrs=(size=8);
xaxis label="Timeline" labelattrs=(size=10) valueattrs=(size=8) max='3Mar2020'd;
refline '1Jan2015'd '1Jan2016'd '1Jan2017'd '1Jan2018'd '1Jan2019'd '1Jan2020'd / axis=x;
refline '3Mar2020'd / axis=x label='03Mar20';
keylegend "Observed proportion" "Modeled trend" "Predicted trend" / location=inside position=topleft across=1 opaque;
run;
What do you get if you set INTERVAL=MONTH on the XAXIS?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.