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 is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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 is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
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: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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