BookmarkSubscribeRSS Feed
dennis_e
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?

 

Thanks,

 

DE

8 REPLIES 8
ChrisHemedinger
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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Reeza
Super User

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

dennis_e
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
Reeza
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.

 

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 55         
 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) 
 

 

 

ballardw
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));

run;

 

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

run;

 

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

 

dennis_e
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. 

Reeza
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. 

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 8 replies
  • 2742 views
  • 7 likes
  • 4 in conversation