BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ubshams
Quartz | Level 8

Hello Everyone,

    I have a time-series graph of two variables up and running.

How do i add/display the cross correlation value to the graph?

I already ran a proc corr prior to these graphs, and can see the correlation value in a table, but would love to add to the graph (if possible).

 

/****following is my code so far***/

title "Variable1 and Variable2 Bivariate Graph with Correlation";

proc sgplot data=out_libactuals_2;

format month mmyy6.;

series x = Month y = Variable1;

series x = Month y = Variable2  y2axis;

run;

quit;

 

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Here is an example using proc timeseries (part of SAS/ETS) to calculate the cross correlation:

data seriesj;
   input x y @@;
   label x = 'Input Gas Rate'
         y = 'Output CO2';
   t + 1;
datalines;
-0.109  53.8  0.000  53.6  0.178  53.5  0.339  53.5
 0.373  53.4  0.441  53.1  0.461  52.7  0.348  52.4
 0.127  52.2 -0.180  52.0 -0.588  52.0 -1.055  52.4
-1.421  53.0 -1.520  54.0 -1.302  54.9 -0.814  56.0
-0.475  56.8 -0.193  56.8  0.088  56.4  0.435  55.7
 0.771  55.0  0.866  54.3  0.875  53.2  0.891  52.3
 0.987  51.6  1.263  51.2  1.775  50.8  1.976  50.5
 1.934  50.0  1.866  49.2  1.832  48.4  1.767  47.9
 1.608  47.6  1.265  47.5  0.790  47.5  0.360  47.6
 0.115  48.1  0.088  49.0  0.331  50.0  0.645  51.1
 0.960  51.8  1.409  51.9  2.670  51.7  2.834  51.2
 2.812  50.0  2.483  48.3  1.929  47.0  1.485  45.8
 1.214  45.6  1.239  46.0  1.608  46.9  1.905  47.8
 2.023  48.2  1.815  48.3  0.535  47.9  0.122  47.2
 0.009  47.2  0.164  48.1  0.671  49.4  1.019  50.6
 1.146  51.5  1.155  51.6  1.112  51.2  1.121  50.5
 1.223  50.1  1.257  49.8  1.157  49.6  0.913  49.4
 0.620  49.3  0.255  49.2 -0.280  49.3 -1.080  49.7
-1.551  50.3 -1.799  51.3 -1.825  52.8 -1.456  54.4
-0.944  56.0 -0.570  56.9 -0.431  57.5 -0.577  57.3
-0.960  56.6 -1.616  56.0 -1.875  55.4 -1.891  55.4
-1.746  56.4 -1.474  57.2 -1.201  58.0 -0.927  58.4
-0.524  58.4  0.040  58.1  0.788  57.7  0.943  57.0
 0.930  56.0  1.006  54.7  1.137  53.2  1.198  52.1
 1.054  51.6  0.595  51.0 -0.080  50.5 -0.314  50.4
-0.288  51.0 -0.153  51.8 -0.109  52.4 -0.187  53.0
-0.255  53.4 -0.229  53.6 -0.007  53.7  0.254  53.8
 0.330  53.8  0.102  53.8 -0.423  53.3 -1.139  53.0
-2.275  52.9 -2.594  53.4 -2.716  54.6 -2.510  56.4
-1.790  58.0 -1.346  59.4 -1.081  60.2 -0.910  60.0
-0.876  59.4 -0.885  58.4 -0.800  57.6 -0.544  56.9
-0.416  56.4 -0.271  56.0  0.000  55.7  0.403  55.3
 0.841  55.0  1.285  54.4  1.607  53.7  1.746  52.8
 1.683  51.6  1.485  50.6  0.993  49.4  0.648  48.8
 0.577  48.5  0.577  48.7  0.632  49.2  0.747  49.8
 0.900  50.4  0.993  50.7  0.968  50.9  0.790  50.7
 0.399  50.5 -0.161  50.4 -0.553  50.2 -0.603  50.4
-0.424  51.2 -0.194  52.3 -0.049  53.2  0.060  53.9
 0.161  54.1  0.301  54.0  0.517  53.6  0.566  53.2
 0.560  53.0  0.573  52.8  0.592  52.3  0.671  51.9
 0.933  51.6  1.337  51.6  1.460  51.4  1.353  51.2
 0.772  50.7  0.218  50.0 -0.237  49.4 -0.714  49.3
-1.099  49.7 -1.269  50.6 -1.175  51.8 -0.676  53.0
 0.033  54.0  0.556  55.3  0.643  55.9  0.484  55.9
 0.109  54.6 -0.310  53.5 -0.697  52.4 -1.047  52.1
-1.218  52.3 -1.183  53.0 -0.873  53.8 -0.336  54.6
 0.063  55.4  0.084  55.9  0.000  55.9  0.001  55.2
 0.209  54.4  0.556  53.7  0.782  53.6  0.858  53.6
 0.918  53.2  0.862  52.5  0.416  52.0 -0.336  51.4
-0.959  51.0 -1.813  50.9 -2.378  52.4 -2.499  53.5
-2.473  55.6 -2.330  58.0 -2.053  59.5 -1.739  60.0
-1.261  60.4 -0.569  60.5 -0.137  60.2 -0.024  59.7
-0.050  59.0 -0.135  57.6 -0.276  56.4 -0.534  55.2
-0.871  54.5 -1.243  54.1 -1.439  54.1 -1.422  54.4
-1.175  55.5 -0.813  56.2 -0.634  57.0 -0.582  57.3
-0.625  57.4 -0.713  57.0 -0.848  56.4 -1.039  55.9
-1.346  55.5 -1.628  55.3 -1.619  55.2 -1.149  55.4
-0.488  56.0 -0.160  56.5 -0.007  57.1 -0.092  57.3
-0.620  56.8 -1.086  55.6 -1.525  55.0 -1.858  54.1
-2.029  54.3 -2.024  55.3 -1.961  56.4 -1.952  57.2
-1.794  57.8 -1.302  58.3 -1.030  58.6 -0.918  58.8
-0.798  58.8 -0.867  58.6 -1.047  58.0 -1.123  57.4
-0.876  57.0 -0.395  56.4  0.185  56.3  0.662  56.4
 0.709  56.4  0.605  56.0  0.501  55.2  0.603  54.0
 0.943  53.0  1.223  52.0  1.249  51.6  0.824  51.6
 0.102  51.1  0.025  50.4  0.382  50.0  0.922  50.0
 1.032  52.0  0.866  54.0  0.527  55.1  0.093  54.5
-0.458  52.8 -0.748  51.4 -0.947  50.8 -1.029  51.2
-0.928  52.0 -0.645  52.8 -0.424  53.8 -0.276  54.5
-0.158  54.9 -0.033  54.9  0.102  54.8  0.251  54.4
 0.280  53.7  0.000  53.3 -0.493  52.8 -0.759  52.6
-0.824  52.6 -0.740  53.0 -0.528  54.3 -0.204  56.0
 0.034  57.0  0.204  58.0  0.253  58.6  0.195  58.5
 0.131  58.3  0.017  57.8 -0.182  57.3 -0.262  57.0
 ;

proc timeseries data=seriesj outcrosscorr=test out=_null_;
var x;
crossvar y;
crosscorr ccf / lags=(0);
run; 

proc sql noprint; 
select CCF format=6.3 into:Crosscorr trimmed 
from test; 
quit;

proc sgplot data=seriesj;
series x=t y=x;
series x=t y=y;
inset ("Crosscorr ="="&Crosscorr");
run;

SGPlot2.png

PG

View solution in original post

7 REPLIES 7
Reeza
Super User
You would need to capture that data and include it in your input data set and then use TEXT statements to have it show on the graph.

Or you could use annotation and an annotation data set, both will accomplish the same thing.
PGStats
Opal | Level 21

Here is an example using proc timeseries (part of SAS/ETS) to calculate the cross correlation:

data seriesj;
   input x y @@;
   label x = 'Input Gas Rate'
         y = 'Output CO2';
   t + 1;
datalines;
-0.109  53.8  0.000  53.6  0.178  53.5  0.339  53.5
 0.373  53.4  0.441  53.1  0.461  52.7  0.348  52.4
 0.127  52.2 -0.180  52.0 -0.588  52.0 -1.055  52.4
-1.421  53.0 -1.520  54.0 -1.302  54.9 -0.814  56.0
-0.475  56.8 -0.193  56.8  0.088  56.4  0.435  55.7
 0.771  55.0  0.866  54.3  0.875  53.2  0.891  52.3
 0.987  51.6  1.263  51.2  1.775  50.8  1.976  50.5
 1.934  50.0  1.866  49.2  1.832  48.4  1.767  47.9
 1.608  47.6  1.265  47.5  0.790  47.5  0.360  47.6
 0.115  48.1  0.088  49.0  0.331  50.0  0.645  51.1
 0.960  51.8  1.409  51.9  2.670  51.7  2.834  51.2
 2.812  50.0  2.483  48.3  1.929  47.0  1.485  45.8
 1.214  45.6  1.239  46.0  1.608  46.9  1.905  47.8
 2.023  48.2  1.815  48.3  0.535  47.9  0.122  47.2
 0.009  47.2  0.164  48.1  0.671  49.4  1.019  50.6
 1.146  51.5  1.155  51.6  1.112  51.2  1.121  50.5
 1.223  50.1  1.257  49.8  1.157  49.6  0.913  49.4
 0.620  49.3  0.255  49.2 -0.280  49.3 -1.080  49.7
-1.551  50.3 -1.799  51.3 -1.825  52.8 -1.456  54.4
-0.944  56.0 -0.570  56.9 -0.431  57.5 -0.577  57.3
-0.960  56.6 -1.616  56.0 -1.875  55.4 -1.891  55.4
-1.746  56.4 -1.474  57.2 -1.201  58.0 -0.927  58.4
-0.524  58.4  0.040  58.1  0.788  57.7  0.943  57.0
 0.930  56.0  1.006  54.7  1.137  53.2  1.198  52.1
 1.054  51.6  0.595  51.0 -0.080  50.5 -0.314  50.4
-0.288  51.0 -0.153  51.8 -0.109  52.4 -0.187  53.0
-0.255  53.4 -0.229  53.6 -0.007  53.7  0.254  53.8
 0.330  53.8  0.102  53.8 -0.423  53.3 -1.139  53.0
-2.275  52.9 -2.594  53.4 -2.716  54.6 -2.510  56.4
-1.790  58.0 -1.346  59.4 -1.081  60.2 -0.910  60.0
-0.876  59.4 -0.885  58.4 -0.800  57.6 -0.544  56.9
-0.416  56.4 -0.271  56.0  0.000  55.7  0.403  55.3
 0.841  55.0  1.285  54.4  1.607  53.7  1.746  52.8
 1.683  51.6  1.485  50.6  0.993  49.4  0.648  48.8
 0.577  48.5  0.577  48.7  0.632  49.2  0.747  49.8
 0.900  50.4  0.993  50.7  0.968  50.9  0.790  50.7
 0.399  50.5 -0.161  50.4 -0.553  50.2 -0.603  50.4
-0.424  51.2 -0.194  52.3 -0.049  53.2  0.060  53.9
 0.161  54.1  0.301  54.0  0.517  53.6  0.566  53.2
 0.560  53.0  0.573  52.8  0.592  52.3  0.671  51.9
 0.933  51.6  1.337  51.6  1.460  51.4  1.353  51.2
 0.772  50.7  0.218  50.0 -0.237  49.4 -0.714  49.3
-1.099  49.7 -1.269  50.6 -1.175  51.8 -0.676  53.0
 0.033  54.0  0.556  55.3  0.643  55.9  0.484  55.9
 0.109  54.6 -0.310  53.5 -0.697  52.4 -1.047  52.1
-1.218  52.3 -1.183  53.0 -0.873  53.8 -0.336  54.6
 0.063  55.4  0.084  55.9  0.000  55.9  0.001  55.2
 0.209  54.4  0.556  53.7  0.782  53.6  0.858  53.6
 0.918  53.2  0.862  52.5  0.416  52.0 -0.336  51.4
-0.959  51.0 -1.813  50.9 -2.378  52.4 -2.499  53.5
-2.473  55.6 -2.330  58.0 -2.053  59.5 -1.739  60.0
-1.261  60.4 -0.569  60.5 -0.137  60.2 -0.024  59.7
-0.050  59.0 -0.135  57.6 -0.276  56.4 -0.534  55.2
-0.871  54.5 -1.243  54.1 -1.439  54.1 -1.422  54.4
-1.175  55.5 -0.813  56.2 -0.634  57.0 -0.582  57.3
-0.625  57.4 -0.713  57.0 -0.848  56.4 -1.039  55.9
-1.346  55.5 -1.628  55.3 -1.619  55.2 -1.149  55.4
-0.488  56.0 -0.160  56.5 -0.007  57.1 -0.092  57.3
-0.620  56.8 -1.086  55.6 -1.525  55.0 -1.858  54.1
-2.029  54.3 -2.024  55.3 -1.961  56.4 -1.952  57.2
-1.794  57.8 -1.302  58.3 -1.030  58.6 -0.918  58.8
-0.798  58.8 -0.867  58.6 -1.047  58.0 -1.123  57.4
-0.876  57.0 -0.395  56.4  0.185  56.3  0.662  56.4
 0.709  56.4  0.605  56.0  0.501  55.2  0.603  54.0
 0.943  53.0  1.223  52.0  1.249  51.6  0.824  51.6
 0.102  51.1  0.025  50.4  0.382  50.0  0.922  50.0
 1.032  52.0  0.866  54.0  0.527  55.1  0.093  54.5
-0.458  52.8 -0.748  51.4 -0.947  50.8 -1.029  51.2
-0.928  52.0 -0.645  52.8 -0.424  53.8 -0.276  54.5
-0.158  54.9 -0.033  54.9  0.102  54.8  0.251  54.4
 0.280  53.7  0.000  53.3 -0.493  52.8 -0.759  52.6
-0.824  52.6 -0.740  53.0 -0.528  54.3 -0.204  56.0
 0.034  57.0  0.204  58.0  0.253  58.6  0.195  58.5
 0.131  58.3  0.017  57.8 -0.182  57.3 -0.262  57.0
 ;

proc timeseries data=seriesj outcrosscorr=test out=_null_;
var x;
crossvar y;
crosscorr ccf / lags=(0);
run; 

proc sql noprint; 
select CCF format=6.3 into:Crosscorr trimmed 
from test; 
quit;

proc sgplot data=seriesj;
series x=t y=x;
series x=t y=y;
inset ("Crosscorr ="="&Crosscorr");
run;

SGPlot2.png

PG
ubshams
Quartz | Level 8
Thanks, that worked
Reeza
Super User
FYI - PROC CORR doesn't calculate cross correlation does it? It does standard correlation and the definition of the two are not the same.
PGStats
Opal | Level 21

Other disciplines, such as engineering and signal processing, have different definitions of the cross-correlation, but SAS sticks with the normalized version which is a generalization of the Pearson coefficient and is always between -1 and +1. At lag=0, the (statistical) cross-correlation and Pearson correlation are mathematically the same. But since time series are often autocorrelated and non-stationnary, and can be related with a lag, time series procs provide tools for estimating the cross correlation after differentiation and/or shifting.

 

If all you need is the 0-lag correlation, you could get it with proc corr.

 

PG
Reeza
Super User
If you manually calculate the lagged variables and use PROC CORR expecting the cross correlation it will differ from what you get from proc timeseries.
PGStats
Opal | Level 21

Formulas are at

 

https://documentation.sas.com/?docsetId=etsug&docsetTarget=etsug_timeseries_details14.htm&docsetVers...

 

Note the denominator of CCF, The variances as those of the whole series, not just the n-nlags pairs of obs. That might account for the difference you are seeing.

PG

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2179 views
  • 5 likes
  • 3 in conversation