/*This code has been written only for demonstration purposes for clients who seek consultants in the area of forecasting and trend analysis. As a sample case of this problem I analyzed the trend of the Walmart stock prices over the last two years. In this code we shall be making use of the chart and the plotting functionalities that shall be used as part of such an analysis. We shall be looking at the trend, the seasonality, the variance of the stock price with the Walmart advertising data. */ /* code for inputting the sample data for Wal Mart on a weekly basis for 2 years. the dif and the lag figures tell */ data sasuser.walmart_returns; infile 'c:\forecastingtraining\walmartreturns.txt'; input date mmddyy10. @11 open 6. high 6. low 6. close 6. volume 35-41 Adj_close 6. advertising 7.; ret_wal=dif2(open)/lag2(open); run; /* checking the correlation between the high and the low figures*/ proc corr; var open high; run; /*plotting the high figures on vbar*/ proc gchart; vbar high; run; /* checking whether the variation in the open values is white noise or not. This we shall get from the chi square analysis for the white noise*/ proc arima data=sasuser.walmart_returns; identify var= open nlag=20; run; /* doing an univariate analysis*/ proc univariate normal; var open; run; /* regression analysis for all variables in the dataset. we shall take a look at regression diferently at a later part of the code*/ proc reg; model _all_=/clm cli; run; /* plot of open and the date of the price tracking*/ proc gplot; plot open*date; run; /* plot of open figures and the date with date being in the desired format*/ proc gplot; plot open*date/overlay; format date date9.; run; /*plot of open and high values of the shares with date using overlay feature*/ proc gplot; plot (open high)*date/overlay; format date date9.; run; /*developing the predicted result set*/ proc reg; model open=high/clb; Output Out=sasuser.wal_Results P=Predreturn; run; /*comparing the predicted result with the adjusted close price figure over a period of time*/ proc gplot data=sasuser.wal_Results; plot (Predreturn adj_close)*Date/overlay; format Date Date9.; run; /* taking a look at the error plot*/ proc reg; model open=high/clb; Output Out=sasuser.wal_Results R=Error; run; /* plot of error with respect to time*/ proc gchart Data=sasuser.wal_Results; vbar Error; run; Proc univariate data=sasuser.wal_Results; var error; probplot; Proc gchart data=sasuser.wal_Results; vbar error; /* checking if the error figures are white noise or not*/ proc arima data=sasuser.wal_Results; identify var=Error; /* this part of the program is for calculating the trend of the Walmart stock price*/ data sasuser.walmart_returns(drop=firstobs); infile 'c:\forecastingtraining\walmartreturns.txt';firstobs=1;/*this latter part of the code was added so that the first row of fictitious data is removed*/ input date mmddyy10. @11 open 6. high 6. low 6. close 6. volume 35-41 Adj_close 6.; Time=_n_;/*declaring time as a variable*/ t2=Time**2;/*various values of time*/ t3=Time**3; t4=Time**4; t5=Time**5; t6=Time**6; t7=Time**7; select(month(date)); when (1,2,3) qtr=1; when(4,5,6) qtr=2; when(7,8,9) qtr=3; otherwise qtr=4; end; run; /*looking at the regression analysis of price with regards to time*/ proc reg; model adj_close= Time T2 T3 T4 T5 T6 T7; Output out=sasuser.walamart_season P=pred; /*looking at the overlay plot with respect to time*/ Proc gplot; Plot(pred adj_close)*Time/overlay; run; /*incorporating the seasonality factor into the regression analysis*/ proc reg; model adj_close= time t2 t3 t4 t5 t6 t7 qtr; output out=sasuser.walmart_season p=predprice; run; /* plotting the predicted price with the adjusted value*/ proc gplot; plot(predprice adj_close)*time/overlay; run; /*looking at the effect of white noise on the predicted model*/ proc arima data=sasuser.walmart_season; identify var=predprice nlag=20; run; /*printing the output as an html and pdf format*/ ods listing close; ods html body='c:\regressionoutput.html'(url='http://sitebuilder.myregisteredsite.com/sitebuilder/regressionoutput.html'); ods pdf file='c:\forecastingtraining\regressionoutput.pdf'; /*incorporating the seasonality factor into the regression analysis*/ proc reg; model adj_close= time t2 t3 t4 t5 t6 t7 qtr; output out=sasuser.walmart_season p=predprice; run; ods _all_ close; ods listing; /*analysis using the advertising data*/ data sasuser.walmart_returns(drop=firstobs); infile 'c:\forecastingtraining\walmartreturns-1.txt';firstobs=1;/*this latter part of the code was added so that the first row of fictitious data is removed*/ input date mmddyy10. @11 open 6. high 6. low 6. close 6. volume 35-41 Adj_close 6. Advertising 7.; format date date9.; time=_n_; t2=time**2; t3=time**3; t4=time**4; t5=time**5; t6=time**6; t7=time**7; t8=time**8; select (month(date)); when (1,2,3) qtr=1; when(4,5,6) qtr=2; when (7,8,9) qtr=3; otherwise qtr=4; end; run; proc reg; model open= qtr time t2 t3 t4 t5 t6 t7 t8 Advertising; output out=work.regress p=pred; run; proc arima data=work.regress; identify var=pred nlag=20; run; proc gplot data=work.regress; plot(open pred)*time/overlay; run;