* BP-1.sas Daniel Brockman 070626 SAS X405.7 project with blood pressure data.; * retested 070805 ; * tested 070804 ; title1 "Blood Pressure Data" ; /* * See assignment "Project of SAS Programming II" by Dr. Jianmin Liu. * * This program effects the project, substituting blood pressure data for * currency data. * * The blood pressure data comes from a previous project. * See http://danielbrockman.com/SAS-X405.7/ for more info on the blood * pressure data. * * This program uses for input a data set created by another program. * This preceding program reads the raw data and detects certain kinds * of errors in the data. (Project items 1 & 3) * * Caveat: BP-1.sas may contain programming errors not exposed by * blood pressure data. If you are thinking of modifications for * use with other data sets, please use a later evolution of the * program, such as unk-1.sas . */ /* * nb. the earliest SAS datevalue is -138061, representing 1-Jan-1582. */ *-------------------------------------------------------; %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="screen"; * for testing lets see it on screen ; %let outfile="&rpt\out-BP-1.txt" ; * output file for corr triangle ; %let DorN=N ; * numeric window selection (see corrwndo()); %let wstart=20 ; * first observation or date for first time window; %let wlen=30 ; * length of time window in days ; %let wintvl=300 ; * length in days of interval between windows ; %let inset=bpset1 ; * input data set; %let inset=bpsetdtkg; %let exset=&inset._ex1 ; * exception list data set ; %let Dv=Sd ; * name of calculated SAS datevalue var ; %let Chrono=Sdt ; * name of date for chronological sort ; %let vvol=Sys Dia Pul WtLb WtKg ; * vars for volatility calc ; %let voth=Hour ; * other variables to maintain ; %let vcor=&Dv &voth &vvol ; * vars for corr calc ; /* ----------------------------------- */ %let avCT=T; * Trading Series ; %let V=5 ; * 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 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. ; /* ----------------------------------- */ %put sub:&sub: inset:&inset ; libname corral "&dat" ; %let tr = tempo00 ; * root name for tempo data set ; %global greturn ; * used for return values from certain macros ; %global greturn1 ; 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 ; * tested * %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"; *-------------------------------------------------------; * 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(corral.&inset,&t1,corral.&exset,&Dv,Year,Month,Day) ; title2 Dataset of Detected Bad Dates ; /* let's do a little report ;*/ * proc contents data=corral.&exset varnum ; * run; title3 First 10 Observations ; proc SQL inobs=10 print ; * 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 &Chrono ; * we may have multi obs on same day at diff times; 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 ; * 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 ; /* %let ii=0; %do %until (%scan(&vvol,%eval(&ii+1)) eq ) ; %let ii=%eval(&ii+1); %let varii=%scan(&vvol,&ii); &varii=log(&varii); * t4: natural logs of corresponding vars in t3 ; %end ; */ 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: &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=C,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 ; /* ================ */