BookmarkSubscribeRSS Feed
Fluorite | Level 6


I am not having luck finding SAS code for plotting a 5 year or 10 year moving average through some climate data.


I have 120 years of heating degree-day data. I want to plot a moving average through the 120 data points.

The data are a list of degree-day values, and a list of years. 120 observations, 2 variables HDD and YEAR. 


I have already plotted the data points over time with proc plot. I also plotted a regression line through the data, however I need to also 


1. plot a solid line (zig zag) that connects the data points

2.plot a 5 or 10 tear moving average theough the data in order to see a smoothed trend


I have not been able to find a smoothing option for plotting data. I am using SAS 9.4 University


Is it possible?





Community Manager

Check out the SERIES plot in PROC SGPLOT for connecting the lines.


For the moving average, @Rick_SAS provides some excellent guidance in this post.  In SAS University Edition you don't have PROC EXPAND (which Rick references), but you do have SAS/IML and of course DATA step.  And you also have PROC TIMEDATA, which can accomplish the moving average computations. See an example in this topic.  PROC TIMEDATA doc here.


Also check out Smoothers for periodic data.

Super User

You can't be using SAS UE and GPLOT or PLOT Procedures unless they suddenly started supporting SAS Graph. 

Fluorite | Level 6
I'm not sure which one I am using. At home I use SAS Studio but at work I use 9.4
Super User

If you're using SAS Graph, GPLOT/PLOT then you are not using SAS University Edition. 


You can run a proc setinit to check this.


proc setinit;run;

Here's what I see if I run that on SAS University Edition, note SAS/GRAPH is not in the list. But perhaps the update fixes that, I clearly haven't done that yet.


 56         proc setinit;run;
 NOTE: PROCEDURE SETINIT used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
 Original site validation data
 Current version: 9.04.01M3P062415
 Site name:    'UNIVERSITY EDITION 2.3 9.4M3'.
 Site number:  70186327.
 CPU A: Model name='' model number='' serial='+2'.
 Expiration:   15JUN2017.
 Grace Period:  0 days (ending 15JUN2017).
 Warning Period: 45 days (ending 30JUL2017).
 System birthday:   09MAR2016.
 Operating System:   LIN X64 .
 Product expiration dates:
 ---Base SAS Software                                                                                    15JUN2017 (CPU A) 
 ---SAS/STAT                                                                                             15JUN2017 (CPU A) 
 ---SAS/ETS                                                                                              15JUN2017 (CPU A) 
 ---SAS/IML                                                                                              15JUN2017 (CPU A) 
 ---SAS/ACCESS Interface to PC Files                                                                     15JUN2017 (CPU A) 
 ---SAS/IML Studio                                                                                       15JUN2017 (CPU A) 
 ---SAS Workspace Server for Local Access                                                                15JUN2017 (CPU A) 
 ---SAS Workspace Server for Enterprise Access                                                           15JUN2017 (CPU A) 
 ---High Performance Suite                                                                               15JUN2017 (CPU A) 



Super User

The plotting procedures will not calculate the moving averages. You will need to calculate the moving average into another variable and add that to the plot. Note that SGPLOT makes that somewhat easier than GPLOT.


A dummy data step, assuming your data is yearly and the number of days is call HDD:


data plot;

   set have;

   HDD05yr = mean(hdd, lag(hdd), lag2(hdd),lag3(hdd),lag4(hdd));

   HDD10yr = mean(hdd, lag(hdd), lag2(hdd),lag3(hdd),lag4(hdd), lag5(hdd), lag6(hdd),lag7(hdd),lag8(hdd),lag9(hdd));



proc sgplot data=plot;

    series x=year y=hdd / <options for your HDD plot>;

    series x=year y=hdd05yr / <options for your 5yr plot>;

    series x=year y=hdd10yr/ <options for your 10yr plot>;



likely options would be markers and or line type and color.


Fluorite | Level 6

Thanks. I will try that on Monday.


It is too bad that SAS does not plot a simple moving average. Moving averages are part of the plotting options in Excel, so I was disappointed that it is not something SAS can do. 

Super User

@dennis_e wrote:

Thanks. I will try that on Monday.


It is too bad that SAS does not plot a simple moving average. Moving averages are part of the plotting options in Excel, so I was disappointed that it is not something SAS can do. 

SAS gives you more options and controls and that comes with a price - more work to specify the features you want.  If you'd like a feature, which is perfectly valid, please add it to the SAS BallotWare as an idea. Mind you once you've programmed it once, it's a trivial task to repeat it as necessary so IMO the trade off is worth it, though I agree, an option would be useful. 


If you have access to SAS via another mechanism with SAS/ETS calculating these values for plotting is a trivial process. 

Fluorite | Level 6
Thank you. This worked.
I'm going to work on the graph line style and labels now.



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. 

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.

Get the $99 certification deal.jpg



Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4 in conversation