Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Graphics
- /
- Formatting and labelling graphs (proc sgplot)

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-21-2022 08:16 AM
(613 views)

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.

- How can I label specific periods on the x-axis?
- What does the statement “fitpolicy” mean and do I need to change anything there?
- I tried changing the “by” command in
*values=(*but it cuts my graph at the end and the labelling is not precise because it distributes my labels at regular intervals.**1**to**63**by**1**)

Thank you for your help!

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

What do you get if you set INTERVAL=MONTH on the XAXIS?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

When I apply INTERVAL=MONTH, it looks quite good and ends the graphic right after the end of the data.

Thank you!

Thank you!

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.