Published on :

Mean_Abs_Deviation Macro

This code is also available in: Deutsch Español Français
Awaiting validation
This macro calculates the Mean Absolute Deviation, defined as the average of the absolute values of the deviations from the mean. The script uses standard procedures (MEANS, TRANSPOSE) and DATA steps to manipulate data structures. It relies on external utility macros (%ranname and %get_number_column_names) for managing variable names and temporary files.
Data Analysis

Type : EXTERNE


Data is provided via the &returns macro parameter. The script expects numerical data representing returns.

1 Code Block
MACRO DEFINITION
Explanation :
Macro definition and initialization of local variables and temporary table names via an external %ranname macro. Identification of numerical columns via %get_number_column_names.
Copied!
1%macro Mean_Abs_Deviation(returns, dateColumn= DATE, outData= mean_abs_dev);
2...
3%let vars= %get_number_column_names(_table= &returns, _exclude= &dateColumn);
4...
2 Code Block
PROC MEANS Data
Explanation :
Calculation of the mean returns for each numerical variable in the input dataset.
Copied!
1 
2PROC MEANS
3DATA= &returns mean noprint;
4OUTPUT out= &meanData;
5RUN;
6 
3 Code Block
DATA STEP Data
Explanation :
Cleaning the statistics table to keep only the means, removing unnecessary statistics (STD, MIN, MAX).
Copied!
1DATA &meanData;
2 SET &meanData;
3 drop _freq_ _type_ &dateColumn;
4 IF _stat_ = 'N' THEN delete;
5...
4 Code Block
PROC TRANSPOSE Data
Explanation :
Transposition of mean tables and raw data (prices/returns) to facilitate subsequent vector calculations.
Copied!
1PROC TRANSPOSE DATA= &meanData out= &meanData;
2RUN;
3...
4PROC TRANSPOSE DATA= &returns(drop=&dateColumn) out= &price_t;
5RUN;
5 Code Block
DATA STEP Data
Explanation :
Merging raw data with their respective means. Using an array (ARRAY) to calculate the absolute value of the difference between each observation and the mean (Abs(Value - Mean)).
Copied!
1DATA &merged;
2 MERGE &price_t &meanData;
3RUN;
4...
5DATA &merged(drop= &i mean);
6 SET &merged;
7 array z[*] &z;
8 DO &i= 1 to dim(z);
9 z[&i]= sum(z[&i], -(Mean));
10 z[&i]= abs(z[&i]);
11 END;
12RUN;
6 Code Block
PROC MEANS Data
Explanation :
Final calculation of the Mean Absolute Deviation (MAD) on the transformed data.
Copied!
1 
2PROC MEANS
3DATA= &merged mean noprint;
4OUTPUT out= &outData;
5RUN;
6 
7 Code Block
PROC DATASETS
Explanation :
Cleaning up temporary tables generated during macro execution.
Copied!
1PROC DATASETS lib= work nolist;
2delete &meandata &merged &price_t;
3RUN;
4QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright (c) 2015 by The Financial Risk Group, Cary, NC, USA.