BookmarkSubscribeRSS Feed
mgm
Fluorite | Level 6 mgm
Fluorite | Level 6

I'm looking to calculate rolling window pairwise correlations, something in SAS that replicates the following python function. Please let me know if you need data and more examples ? 

 

Proc IML  doesn't do the trick either : 

proc iml;
	use work.ret_trans;
	read all var{date} into myDates;		
	read all var _NUM_ into X[colname=NumerNames];
do x=18 to nrow(myDates);
		dt=myDates[x];
		lagdt = intnx('month',dt,-18,'end');
		read all var _NUM_ where(date <= dt & date > lagdt ) into trailing18;
		
		idx = setdif(1:ncol(trailing18), 1); 
		
	pc = corr(trailing18);
	
	if x=18 then create work.PairWise from pc ;
		else edit work.PairWise ;
		append from pc;
	
  end;
quit;

 

pandas.rolling_corr pandas.rolling_corr(arg1, arg2=None, window=None, min_periods=None, freq=None, center=False, pairwise=None, how=None)

Moving sample correlation.

 

 

Parameters: Returns:

arg1 : Series, DataFrame, or ndarray

arg2 : Series, DataFrame, or ndarray, optional

if not supplied then will default to arg1 and produce pairwise output

window : int

Size of the moving window. This is the number of observations used for calculating the statistic.

min_periods : int, default None

Minimum number of observations in window required to have a value (otherwise result is NA).

freq : string or DateOffset object, optional (default None)

Frequency to conform the data to before computing the statistic. Specified as a frequency string or DateOffset object.

center : boolean, default False

Set the labels at the center of the window.

how : string, default ‘None’

Method for down- or re-sampling

pairwise : bool, default False

If False then only matching columns between arg1 and arg2 will be used and the output will be a DataFrame. If True then all pairwise combinations will be calculated and the output will be a Panel in the case of DataFrame inputs. In the case of missing elements, only complete pairwise observations will be used.

y : type depends on inputs

DataFrame / DataFrame -> DataFrame (matches on columns) or Panel (pairwise) DataFrame / Series -> Computes result for each column Series / Series -> Series

 

1 REPLY 1
Rick_SAS
SAS Super FREQ

If you have SAS/ETS software, you can use PROC EXPAND. See the second example in Vora (2008):

http://www2.sas.com/proceedings/forum2008/093-2008.pdf

 

A DATA step solution is provided in the same paper.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1 reply
  • 1920 views
  • 2 likes
  • 2 in conversation