* fx2.sas Daniel Brockman 070805 SAS X405.5 project with fx2 data.; * tested 070805 ; title1 "Currency Prices (unknown currencies), provided by Mr. Mark Gschwind" ; /* * See assignment "Project of SAS Programming II" by Dr. Jianmin Liu. * * This program effects the project, using currency data from file * "dummyfx2.txt", provided by Mr. Mark Gschwind. * * This program uses for input a text file that contains 3 time series of * what may be currency prices. We proceed to analyze them as such. * * This program evolved from unk-1.sas . * */ /* * nb. the earliest SAS datevalue is -138061, representing 1-Jan-1582. */ *-------------------------------------------------------; %global greturn ; * used for return values from certain macros ; %global greturn1 ; *-------------------------------------------------------; %let dir=C:\b\SAS-X405.5 ; * root directory ; *-------------------------------------------------------; %let prg=&dir\prg ; * program subdirectory ; %let sub=&dir\sub; * subdir containing autoloaded macros ; %let dat=&dir\data ; * data subdirectory ; %let rpt=&dir\rpt ; * reports and outputs subdirectory ; *-------------------------------------------------------; %let outfile="&rpt\out-fx2.txt" ; * output file for corr triangle ; %let outfile="screen" ; * lets see it on screen ; /* * See %annvol() for detailed description of choices for * avCT, V and ann. But to oversimplify, * 1. For a trading series, in which you skip saturdays & sundays, assign * avCT=T, V=5 (for volatility measured over a week), ann=250 . * 2. For a conventional calendar that includes all days of the year, assign * avCT=C, V=7 (for volatility measured over a week), and ann=365.25 . * 3. Other choices: * For V, you may choose any integer >=2. * For ann, you may choose any positive number. * For avCT, only T and C produce defined results. */ /* ----------------------------------- */ %let avCT=C; * Conventional Calendar ; %let V=7 ; * num of days for calc of annualized volatility; %let ann=365.25 ; * days/yr assumption for calc of ann'zed vol ; %let CalNote=Conventional Calendar. ; %let avCT=T; * Trading Series ; %let V=60 ; * num of days for calc of annualized volatility; %let ann=250 ; * days/yr assumption for calc of ann'zed vol ; %let CalNote=Trading Calendar. ; /* ----------------------------------- */ %let DorN=N ; * numeric window selection (see corrwndo()); %let wstart=1 ; * first observation or date for first time window; %let wlen=125 ; * length of time window in days ; %let wintvl=0 ; * length in days of interval between windows ; libname corral "&dat" ; * %let inset=corral.bpset1 ; * input data set; * %let inset=corral.bpsetdtkg; %let infile="&dat\dummyfx2.txt" ; * input data file ; %let exset=dummyfx2_ex1 ; * exception list data set ; %let Dv=Sd ; * name of calculated SAS datevalue var ; /* Chrono is a SAS datetimevalue or a SAS timevalue that allows * for a chronological sort of observations that occur on the same * date. Chrono isnt a required variable in the data set. If Chrono * isnt present in the input, then assign null to Chrono. 070805 */ %let Chrono=Sdt ; * name of datetimevalue for chronological sort ; %let Chrono= ; * We have no time of day in dummyfx2 ; %let tr = fx2 ; * root name for tempo data set ; options ls = 80 mprint mlogic symbolgen sasautos="&sub" /* doublequotes required */ spool missing=. ; proc printto; run; /* restore default destinations */ * macros not in autocall library and macros in test ; * %include "&sub\cvtymdsas.sas" ; * %include "&sub\findavar.sas" ; * %include "&sub\getdsname.sas" ; * %include "&sub\mkvarlist2.sas" ; * %include "&sub\dtchkf.sas" ; * %include "&sub\corrwndo.sas" ; * %include "&sub\wndo2.sas" ; * %include "&sub\trimset.sas" ; * %include "&sub\annvol.sas" ; * %include "&sub\cchg.sas" ; * %include "&sub\dtchkf.sas" ; * %include "&sub\consecdays.sas"; * %include "&sub\clog.sas"; /* ------------------------------------------------------------ */ /* ---- These var assignments must wait for inclusion of macros */ %let vvol=cur1-cur3 ; * vars for volatility calc ; %deabbr(&vvol); * expand this abbrevation ; %let vvol=&greturn; * expanded form; %let voth= ; * other variables to maintain ; %let vcor=&Dv &voth &vvol ; * vars for corr calc ; /* ------------------------------------------------------------ */ *-------------------------------------------------------; /* Let's get this underway */ * read input into a SAS data set from the text file raw data; /* Example lines from the input file: * 01/06/2006|0.822977533|114.44|1.638 * 01/09/2006|0.827266711|114.48|1.6347 * 01/10/2006|0.828912467|114.33|1.6342 * 01/11/2006|0.824606251|114.15|1.6287 * 01/12/2006|0.830840811|114.37|1.6274 */ %getdsname(&tr); * get tempo dataset name ; %let t0=&greturn; data &t0 ; infile &infile DLM='|'; * ye olde pipe delimiter ; input emdeewye $10. &vvol ; mm=put(substr(emdeewye,1,2),$2.); * create checkable month ; Month=input(mm,2.) ; dd=put(substr(emdeewye,4,2),$2.); * create checkable day ; Day=input(dd,2.) ; yy=put(substr(emdeewye,7,4),$4.); * create checkable year ; Year=input(yy,4.) ; &Dv=input(emdeewye,mmddyy10.); * get our index date (if we can).; format &Dv yymmdd10. ; run; *title2 "Test examination of input data" ; *proc print data=&t0 ; run; /* test */ *title2 ; /* test */ %let inset=&t0 ; * <<<<================ reassignment of inset ; * Check dates, put err msg in Log, put bad data in exception dataset, ; * replace bad date with missing value ; %getdsname(&tr); * get tempo dataset name ; %let t1=&greturn; * output set fr dtchkf ; /* We'll use tempo datasets hereafter to avoid touching our orig data. ;*/ %dtchkf(&inset,&t1,&exset,&Dv,Year,Month,Day) ; title2 Dataset of Detected Bad Dates ; /* let's do a little report ;*/ title3 " First 10 Observations from input set &inset " ; proc print data=&exset ; run ; /* * in this case, proc print makes the better report proc SQL inobs=10 print obs ; * 10 lines of bad dates ; select * from corral.&exset quit; */ title2; title3; * Put observations in chronological order in tempo dataset &t2 . ; %getdsname(&tr); * get tempo dataset name ; %let t2=&greturn; proc sort data=&t1 out=&t2 ; by &Dv &Chrono ; * 070805 ; run; * Eliminate duplicate dates, keeping the earliest ; proc sort data=&t2 nodupkey; by &Dv ; * Dv is SAS datevalue ; run; * Our data is a time series. Insert dates for missing observations ; %getdsname(&tr); * get tempo dataset name ; %let t3=&greturn; %consecdays(&t2,&t3,&Dv) ; * perform insertion ; * %let t4=&t3 ; * We calc logs for fx2 ; * Calc logarithms ; %getdsname(&tr); * get tempo dataset name ; %let t4=&greturn; data &t4 (keep=&Dv &voth &vvol) ; * Note: pruning the vars in the dataset ; set &t3 ; %clog(&vvol); * Cant do this calc in open code ; run; * Calculate changes in vars of interest ; %getdsname(&tr); * get tempo dataset name ; %let t5=&greturn; %cchg (&t4,&t5,&Dv,&voth,&vvol,&avCT) ; * t5 contains diffs of logs; * trim useless leading and trailing empty obs from dataset ; %getdsname(&tr); * get tempo dataset name ; %let t7=&greturn; %trimset(&t5,&t7,&vvol) ; %put "number of observations in t7 (&t7): &greturn" ; * Annualized volatility of random vars measured over V days. ; %getdsname(&tr); * get tempo dataset name ; %let t6=&greturn; %annvol(iset=&t7,oset=&t6,Dv=&Dv,voth=&voth,vlist=&vvol, cal=&avCT,ndy=&V,anndy=&ann) ; /* Loop through the calculated volatilities, select time windows, * calculate correlation matrix and print its lower triangle. */ title2 "&CalNote Annualized &V.-day volatilities. DPY assumption:&ann "; %corrwndo(&t6,&Dv,&vcor,&DorN,&wstart,&wlen,&wintvl,&outfile); title2 ; proc printto; run; %getdsname(&tr); %let t8=&greturn ; data &t8; if ( not (&outfile = "screen")) then do ; Report_File= &outfile ; output ; end; proc print data=&t8 noobs ; run; * clean up tempo datasets ; options nomlogic nomprint nosymbolgen ; * we dont need to see this chatter ; %delds(&t0); %delds(&t1); %delds(&t2); %delds(&t3); %delds(&t4); %delds(&t5); %delds(&t6); %delds(&t7); %delds(&t8); /* restore */ options mlogic mprint symbolgen ; /* ================ */