BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
samp945
Obsidian | Level 7

Hello all,

 

I have a table of time-series data produced with proc UCM. It includes actual values, forecast values, upper confidence limit values, and lower confidence limit values.

 

My code produces the output I want except for one thing: I'd like the confidence limit values (band plot) to appear only after the refline. Values for the upper and lower confidence limits exist in the table prior to the refline date. I could accomplish the desired graph with a data step prior to sgplot where I simply remove confidence limit values in the table prior to the refline date, but I wonder if there is a better way.

 

Proc UCM's plot option produces the forecast plots this way, but I want to use sgplot to customize the graphs myself.

 

Thanks for your advice!

 

proc sgplot data=TableAll noborder;

			format COUNT comma6.;

	band x=Month lower=LCL upper=UCL /
		legendlabel="95% Confidence Limits"
		name="CL";

	series x=Month y=Forecast /
		smoothconnect
		lineattrs=(thickness=1 color=black)
		legendlabel="Forecast"
		name="Forecast";

	scatter x=Arr_Month y=Count /
		legendlabel="Actual"
		name="Actual";

	refline '01Jan2020'd /
		axis=x
		lineattrs=(thickness=2 color=darkred pattern=dash)
		label=("Forecasts /Begin")	splitchar="/";

	keylegend "Actual" "Forecast" "CL" /
	Title="" NOBORDER;

	yaxis label="Monthly Count"
		grid values=(0 to 80000 by 10000);
		
	xaxis display=(nolabel)
		type=time
		grid values=('1Jan2015'd to '31Dec2024'd by year);
run;

Screenshot.png

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Set LCL and UCL to a missing for the months prior to the cutoff date (01JAN2020). Or, assign LCL and UCL to the predicted value prior to the cutoff. The difference between these two choices determines whether the band region "flares out" at the cutoff date, or appears suddenly after the cutoff date. your choice.

You didn't provide data, but here is an example:

 

/* create fake data */
data Have;
format Day Date10.;
do Day = '01Jan2019'd to '01Mar2020'd by 7;
   pred = (Day-'01Jan2019'd)/50 + sin(Day/10);   
   LCL = pred - 1;
   UCL = pred + 1;
   output;
end;
run;

title "Sample Band Plot";
proc sgplot data=Have;
  band x=Day Lower=LCL upper=UCL;
  series x=Day y=Pred;
run;

%let cutoff = '01Jan2020'd;
data Want;
set Have;
if Day < &cutoff then do;
   LCL = pred;  /* or assign to missing value */
   UCL = pred;
end;
run;

title "Only Show Band On and After Cutoff";
proc sgplot data=Want;
  band x=Day Lower=LCL upper=UCL;
  series x=Day y=Pred;
  refline &cutoff / axis=x;
run;


Rick_SAS_0-1725887251580.png

 

View solution in original post

6 REPLIES 6
samp945
Obsidian | Level 7

Thanks for this link - some good tips in there, and perhaps they could be used to accomplish my goal, but I think it would be more complicated than just using a data step and removing pre-refline confidence limits.

Quentin
Super User

I think easiest approach would be to add a DATA step to delete the LCL / UCL values before the refline, as you suggest.  I don't know PROC UCM, I suppose it's worth checking if there is a PROC UCM option that would only calculate the LCL / UCL for the forecast, not the actual.  

 

But for the SGPLOT, I don't think there is a way to say make this BAND:

	band x=Month lower=LCL upper=UCL /
		legendlabel="95% Confidence Limits"
		name="CL";

but only for Month values >= Jan 2020.

I suppose if you really wanted to you could try to add some sort of dynamic formatting to make the band color transparent before that date.  But I think it would be easier and clearer code if you go with the DATA step approach.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
samp945
Obsidian | Level 7

It may indeed be easiest to just use a data step. I thought that maybe there is a sgplot option that I'm not familiar with to restrict certain plots to certain time periods. Maybe the closest sgplot strategy is to change the appearance of portions of a time period as user Ksharp linked to above. I think that approach could be used to make pre-refline bands transparent (invisible), but that would require more fussing than just using a data step and removing the confidence limit values prior to the refline time.

 

With that said, the plot produced by PROC UCM gives the ouput I want (attached below). I just don't know how it does this.

ForecastsPlot14.png

Quentin
Super User

I suppose it's possible that PROC UCM is using a different dataset to make that plot than the output dataset you are using. Maybe you could try running the PROC UCM  step inside an ODS TRACE ON sandwich to see if there are any other datasets generated which might have the data in the format you want.  Often the datasets you can get from OUTPUT statements / OUT options are different than the "internal" datasets that are exposed via ODS.

 

PROC UCM should be using a GTL template to make the plot.  I guess it's possible there is some GTL magic which allows you to make this sort of partial bland plot, but I don't remember it.  (I don't use GTL much anymore, I find the SG procs can do everything I need). You can use ODS TRACE ON to see which graph template PROC UCM is using, then you can use PROC TEMPLATE to view the template:

proc template;
source Base.Freq.Graphics.OneWayFreqDotPlot; *this is for FREQ, replace with template used by PROC UCM to make the plot;
run;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Rick_SAS
SAS Super FREQ

Set LCL and UCL to a missing for the months prior to the cutoff date (01JAN2020). Or, assign LCL and UCL to the predicted value prior to the cutoff. The difference between these two choices determines whether the band region "flares out" at the cutoff date, or appears suddenly after the cutoff date. your choice.

You didn't provide data, but here is an example:

 

/* create fake data */
data Have;
format Day Date10.;
do Day = '01Jan2019'd to '01Mar2020'd by 7;
   pred = (Day-'01Jan2019'd)/50 + sin(Day/10);   
   LCL = pred - 1;
   UCL = pred + 1;
   output;
end;
run;

title "Sample Band Plot";
proc sgplot data=Have;
  band x=Day Lower=LCL upper=UCL;
  series x=Day y=Pred;
run;

%let cutoff = '01Jan2020'd;
data Want;
set Have;
if Day < &cutoff then do;
   LCL = pred;  /* or assign to missing value */
   UCL = pred;
end;
run;

title "Only Show Band On and After Cutoff";
proc sgplot data=Want;
  band x=Day Lower=LCL upper=UCL;
  series x=Day y=Pred;
  refline &cutoff / axis=x;
run;


Rick_SAS_0-1725887251580.png

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 632 views
  • 0 likes
  • 4 in conversation