SAS Programming

2,226 views

Published on

Currency Volatility Analysis

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,226
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
53
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SAS Programming

  1. 1. /*CURRENCY VOLATILITY PROJECT Author: Phil Duhe, November, 2009 ***************************PART A******************************************/ *Create two temporary data sets that contain the original data in the text files Data1.txt and Data2.txt; *system options for page appearance; *Establish a file reference for data1.txt; filename DATA1 'C:NewSASDATA1.TXT'; *reading the data from data1.txt; data A; infile DATA1 firstobs=7; input date yymmdd6. dow bp cd dm; *Change all the 0 values to missing; if bp <= 0 then bp = .; if cd <= 0 then cd = .; if dm <= 0 then dm = .; run; *establish file reference for data2.txt; filename DATA2 'C:NewSASDATA2.TXT'; *reading the data from data2.txt; data B; infile DATA2 firstobs=7; input date: yymmdd6. dow jy sf; *Change all the 0 values to missing; if jy <= 0 then jy = .; if sf <= 0 then sf = .; run; ************************END OF PART A*********************************; ***************************PART B*************************************; *Sorting data for merge; proc sort data=A; by date dow; run; proc sort data=B; by date dow; run; *************************PART B C D &E**********************************; *Creating permanent merged data set MyLib.MergedData; options ls=80 ps=60 pageno=1 nodate center; libname MyLib 'C:SASIntro'; data MyLib.MergedData; merge A(in=ina) B(in=inb); by date dow; if ina and inb;*merged set contains nonmissing observations in A and B; format date mmddyy8. dow 2.0 bp--sf 7.6; *Labeling the variables(PART E);
  2. 2. label date = 'Date' dow = 'Day of Week' bp = 'British Pound' cd = 'Canadian Dollar' dm = 'Deutsche Mark' jy = 'Japanese Yen' sf = 'Swiss Franc'; run; /*Print output*/ proc print data=MyLib.MergedData Width=FULL label; title 'Merged Data Set'; *show date and not #observations; id date; var dow bp--sf; run; ******************************END OF PART B D &E****************************; ***********************************PART C*************************************; data MyLib.LogResult (drop=bp cd dm jy sf zbp zcd zdm zjy zsf); set MyLib.MergedData; zbp = bp / (lag(bp)) ; zcd = cd / (lag(cd)) ; zdm = dm / (lag(dm)) ; zjy = jy / (lag(jy)) ; zsf = sf / (lag(sf)) ; *Calculate the log of daily returns(PART C); lgbp = log(zbp); lgcd = log(zcd); lgdm = log(zdm); lgjy = log(zjy); lgsf = log(zsf); *Format and label the variables; format date mmddyy8. dow 2.0 lgbp--lgsf 7.6 ; *Labeling the variables(PART E); label date='Date' dow ='Day of Week' lgbp='British Pound' lgcd='Canadian Dollar' lgdm='German Deutschmark' lgjy='Japanese Yen' lgsf='Swiss Franc'; run; options ls=80 ps=60 pageno=1 nodate center; proc print data=MyLib.LogResult Width=Full label; title 'Log of Daily Returns'; id date; var dow lgbp--lgsf; run; *Volatility PART C; /*Generate standard deviations of random variables
  3. 3. create a new data set based on PROC MEANS*/ proc means data=MyLib.LogResult std n noprint;*no need to print means statistics; var lgbp--lgsf; *Outputting the Results into the dataset VolResult; output out=MyLib.VolResult(drop= _TYPE_ _FREQ_) std=S_bp S_cd S_dm S_jy S_sf n = N_bp N_cd N_dm N_jy N_sf; run; /*Print to verify standard deviations*/ proc print data=MyLib.VolResult Width=Full label; options ls=80 ps=60 pageno=1 nodate center; title 'Standard Deviations of Log of Daily Returns'; var S_bp--S_sf; run; *Calculate the estimate of annual volatility; data MyLib.AnnVol(drop=S_bp S_cd S_dm S_jy S_sf N_bp N_cd N_dm N_jy N_sf); set MyLib.VolResult; *Annualized volatility calculation; Vol_bp =S_bp*sqrt(250/N_bp); Vol_cd =S_cd*sqrt(250/N_cd); Vol_dm =S_dm*sqrt(250/N_dm); Vol_jy =S_jy*sqrt(250/N_jy); Vol_sf =S_sf*sqrt(250/N_sf); label Vol_bp = 'British Pound' Vol_cd = 'Canadian Dollar' Vol_dm = 'German Deutschmark' Vol_jy = 'Japanese Yen' Vol_sf = 'Swiss Franc'; run; proc print data=MyLib.AnnVol Width= FULL label; title1 'Annualized Foreign Exchange Rate Volatilty'; title2; title3 'Major Trading Currencies'; run; ********************************END OF PART C******************************; *******************************PART F**************************************; /*Create an external file in (ASCII format) using PUT statement and appropriate FORMAT statement*/ proc sort data=MyLib.LogResult; by date dow; run; Data _null_; set MyLib.LogResult; file 'C:UsersPhilSasProject'; **write merged data to output file; if _n_=1 then do;*do loop to establish report headings and column spacings; * report headings; put 'SAS X405, UCBX, November 2009, Term Project'; put 'Phil Duhe';
  4. 4. put ' '; put 'Natural Logarithm of Daily Returns'; put 'of Five Currencies'; put 'June, 1973 to November, 1987'; put ' '; put ' '; put ' '; put ' ' ' British' ' Deutsche' ' Canadian' ' Japanese' ' Swiss' ; put ' ' ' Pounds' ' Marks' ' Dollars' ' Yen' ' Franc' ; put ' '; put ' Date' ' LGBP' ' LGCD' ' LGDM' ' LGJY' ' LGSF' ; put ' - '; end; * end if 1st obs; put @2 date MMDDYY8. @15lgbp 7.6 @25lgcd 7.6 @35lgdm 7.6 @45lgjy 7.6 @55lgsf 7.6; run; ******************************END OF PART F**********************************; ********************************PART G***************************************; /*Calculate the variance-covariance matrix of the daily return of random variables*/ /*Note: The variance of a variable is a measure of the dispersion of the variable around its mean value.*/ /*Note: Covariance is a measure of the strength of the link between two random numerical variables*/ ods select Cov; proc corr data=MyLib.LogResult cov nocorr nosimple;*just print the variance- covariance matrix; options ls=180 ps=65 pageno=1 nodate center;*landscape; title1 'Variance-Covariance Matrix'; title2; title3 'Log of Daily Returns of Major FX Currencies';
  5. 5. var lgbp--lgsf; run; **********************************END OF PART G*****************************; ************************************PART H***********************************; /*calculate the variance-covariance matrix for Mondays*/ data MyLib.Monday; set MyLib.LogResult; where dow EQ 1;*read input data set for Monday only; run; ods select Cov; proc corr data=MyLib.Monday cov nocorr nosimple ;*print covariances and not descriptive statistics; options ls=180 ps=60 pageno=1 nodate nocenter;*landscape; title 'Monday Trading Days'; var lgbp--lgsf; run; /*calculate the variance-covariance matrix for Thursdays*/ data MyLib.Thursday; set MyLib.LogResult; where dow EQ 4;*read input data set for Thursday only; run; ods select Cov; proc corr data=MyLib.Thursday cov nocorr nosimple;*print covariances and not descriptive statistics; options ls=180 ps=60 pageno=1 nodate nocenter;*landscape; title 'Thursday Trading Days'; var lgbp--lgsf; run; ***********************************END OF PART H**************************; **************************************PART I******************************; /*Calculate the correlation coefficient matrix of the log of daily returns*/ proc corr data=MyLib.LogResult Pearson nosimple nomiss rank; options ls=180 ps=60 pageno=1 nodate nocenter;*landscape; title1 'Correlation Coefficient Matrix'; title2; title3 'Log of Daily Returns of Major FX Currencies'; var lgbp--lgsf; run; ***********************************END OF PART I***************************; **************************************PART J*******************************; /*Calculate the skewness, kurtosis of the log of daily return of all the random variables*/ /*Skewness: measures the tendency of the deviations to be larger in one direction than in the other. The sample sknewness can be either positive or negative. Observations that have a normal distribution should have a sknewness near zero(0).*/
  6. 6. /*Kurtosis: measures the heaviness of tails of the data distribution. Observations that are normally distributed should have a kurtosis near zero(0).*/ proc means data=MyLib.LogResult skewness kurtosis; options ls=80 ps=60 pageno=1 nodate center; title 'Skewness and Kurtosis of Log of Daily Returns'; var lgbp--lgsf; run; **********************************END OF PART J*************************; ****************OPTIONAL************************************************; /*EXERCISE K*/ /*Write a macro program so that you can repeatedly excecute correlation coefficients for different data sets without retyping the procedure*/ %macro example(request= , mydata= );*parameter variables set to null; %if &request=print %then %do; proc print data=&mydata;*print data set; run; %end; %else %if &request=correlation %then*run the correlation; %do; proc corr data=&mydata pearson nosimple nomiss rank;*rank the correlations; run; %end; %mend example; ***********************************************************************; *Example of Calling MACRO Example; %example(request=correlation, mydata=MyLib.LogResult)*pass in parameter variables; ***********************************************************************; /*Extra Programs*/ /*Univariate Statistics for Log of Daily Returns*/ ods select BasicMeasures ExtremeObs;*Basic statistics and extreme observations for nominal value of fx currencies; title "Basic Measures"; proc univariate data=MyLib.LogResult; var lgbp--lgsf; run; title "Log of Daily Returns vs. Normal Distribution"; proc univariate data=MyLib.LogResult noprint; var lgbp--lgsf; histogram lgbp--lgsf/normal (color=red w=2) ; inset mean='Mean' (7.6) std ='Standard Deviation' (7.6)/font='Arial' pos=nw height=3;
  7. 7. run; *******************************************************************; ods html; ods graphics on; proc corr data=MyLib.LogResult nomiss noprint plots=matrix; var lgbp--lgsf; title 'Correlation Coefficients Using PROC CORR'; run; ods graphics off; ods html close; ********************************************************************; ods html; ods graphics on; proc corr data=MyLib.LogResult nomiss noprint plots=scatter(alpha = .20 .30); var lgbp--lgsf; run; ods graphics off; ods html close;

×