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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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