# SAS/ETS Examples

## Calculating Price Elasticity of Demand

Contents | Back to Example

# SAS Program

```   /*-----------------------------------------------------------------
Example: Calculating Price Elasticity of Demand
Requires: SAS/ETS, SAS/IML
Version: 9.0
------------------------------------------------------------------*/

data a ;
input yr qtr q p ;
date = yyq(yr,qtr) ;
format date yyq6. ;
lq = log(q) ;
lp = log(p) ;
datalines;
1977   1   22.9976   142.1667
1977   2   22.6131   143.9333
1977   3   23.4054   146.5
1977   4   22.7401   150.8
1978   1   22.0441   160
1978   2   21.7602   182.5333
1978   3   21.6064   186.2
1978   4   21.8814   186.4333
1979   1   20.5086   211.7
1979   2   19.0408   231.5
1979   3   19.1457   222.7
1979   4   19.3989   223.8333
1980   1   18.947    231.1667
1980   2   18.898    227.5
1980   3   19.2127   237.5333
1980   4   19.4988   238.1667
1981   1   19.3429   233.5
1981   2   18.9816   230.7
1981   3   19.6459   239.0333
1981   4   19.2868   235.4333
1982   1   18.8193   233.3
1982   2   18.7968   242.9667
1982   3   19.9882   244.0333
1982   4   19.4242   233.1333
1983   1   19.2252   233.8667
1983   2   19.3361   240.9333
1983   3   20.4952   234.3667
1983   4   19.5995   227.1667
1984   1   19.3191   238.4667
1984   2   19.3744   238
1984   3   19.9738   232.2
1984   4   19.7597   233.2333
1985   1   19.0586   234.9
1985   2   20.0757   230.4333
1985   3   20.8175   222.7333
1985   4   19.2488   226.4333
1986   1   19.0212   229.2667
1986   2   20.1607   222.9
1986   3   20.7108   225.6333
1986   4   18.9416   229.3
1987   1   18.2746   230.6
1987   2   18.5266   239.1
1987   3   19.175    242.1667
1987   4   17.8807   241.6667
1988   1   18.1678   241.7333
1988   2   18.4639   250.1
1988   3   18.7622   254.5333
1988   4   17.2552   255
1989   1   16.9765   260.7
1989   2   17.5192   266.9667
1989   3   17.5975   268.0333
1989   4   17.2416   266.9333
1990   1   16.5516   272.6333
1990   2   17.3763   281.2
1990   3   17.4207   280.3667
1990   4   16.4315   289.8667
1991   1   15.9842   294.2667
1991   2   17.0449   295.2
1991   3   17.5591   284.6333
1991   4   16.2093   279.2
1992   1   16.3952   282.2667
1992   2   16.9538   286.8333
1992   3   17.227    282.6667
1992   4   15.9093   286.6667
1993   1   15.8915   292.1333
1993   2   16.2058   300.4
1993   3   17.043    292
1993   4   15.9513   289.2333
1994   1   16.2494   286.6667
1994   2   16.8868   286.1667
1994   3   17.3775   279.5
1994   4   16.4723   279.1667
1995   1   16.3097   283.8667
1995   2   17.0895   283.1
1995   3   17.647    285.1
1995   4   16.3476   285.2667
1996   1   17.0783   278.7
1996   2   17.6763   277.4
1996   3   17.1606   279.8
1996   4   16.2764   285.0333
1997   1   16.2921   278.8
1997   2   17.2994   278.9667
1997   3   17.0513   281.0667
1997   4   16.2354   279.3
1998   1   16.6884   273.4667
1998   2   17.1985   278.1
1998   3   17.5085   277.3667
1998   4   16.6475   279.5333
1999   1   16.6785   278
1999   2   17.7635   284.7667
1999   3   17.6689   289.2333
;
run ;

proc autoreg data=a outest=estb ;
model lq = lp ;
output out=out1 r=resid1 ;
title "OLS Estimates";
run ;

proc autoreg data=a outest=estb ;
model lq = lp  / nlag=(1 4) method=ml ;
output out=out2 r=resid2 ;
title "OLS, Autocorrelations, & ML";
run ;

data plot2 ;
merge out1 out2 a ;
run ;

proc gplot data=plot2 ;
title 'OLS Model Residual Plot' ;
axis1 label=(angle=90 'Residuals') ;
axis2 label=('Date') ;
symbol1 c=blue i=needle v=none ;
plot resid1*date / cframe=ligr haxis=axis2 vaxis=axis1 ;
run ;

proc autoreg data=a outest=estb ;
model lq = lp  / nlag=(1 4) method=ml ;
output out=out2 r=resid2 ;
title "OLS, Autocorrelations, & Maximum Likelihood Estimates";
run ;

proc gplot data=plot2 ;
title 'Autoregressive Error Model Residual Plot' ;
axis1 label=(angle=90 'Residuals') ;
axis2 label=('Date') ;
symbol1 c=blue i=needle v=none ;
plot resid2*date / cframe=ligr haxis=axis2 vaxis=axis1 ;
run ;

proc iml ;
use estb ;
read all var {intercept lp} ;
close estb ;
x = (100:300)` ;
x = log(x) ;
y = intercept + lp*x ;
all = y || x ;
create outset from all ;
append        from all ;
quit ;
data plot ;
set outset(rename=(col1=y col2=x)) ;
run ;
proc gplot data=plot ;
title 'Demand for Beef' ;
axis1 label=(angle=90 'log of quantity') ;
axis2 label=('log of price') ;
symbol color=blue interpol=join value=none ;
plot y*x / cframe=ligr vaxis=axis1 haxis=axis2 ;
run ;

```