Linear interpolation implies fitting joined, straight line segments between adjacent points in your data and then, for any new X value, obtaining its Y value from the line segment above it. This can be done using PROC EXPAND in SAS/ETS or using PROC TRANSREG in SAS/STAT as shown later in this note.
Suppose you have the following data with observed values of Y at X=0, 1, 2, 3, and 4. You are interested in getting linearly interpolated values at some intermediate points (X=0.7, 1.3, 2.2, 2.6, and 3.9) that appear in the data set with missing values for Y.
data mydata;
input x y;
datalines;
0.0 1
0.7 .
1.0 3
1.3 .
2.0 4
2.2 .
2.6 .
3.0 6
3.9 .
4.0 10
;
Using PROC EXPAND
To linearly interpolate between the observed data points use PROC EXPAND in SAS/ETS. Note that the data must be sorted by the ID variable if it is not already. Also, only missing values that fall between non-missing values can be interpolated. Missing observations before the first non-missing value or after the last non-missing value will not be estimated.
proc sort data=mydata;
by x;
run;
proc expand data=mydata out=LinInterp;
convert y=linear / method=join;
id x;
run;
A plot of the data shows that the new points, denoted by blue stars, fall on straight line segments between the observed data points and therefore are linearly interpolated between adjacent observed values.
symbol1 v=dot i=join c=red;
symbol2 v=star i=none c=blue;
proc gplot data=LinInterp;
plot y*x=1 linear*x=2 / overlay;
run; quit;
The LINEAR variable created by PROC EXPAND contains the linearly interpolated values for the new data points as well as the Y values of the original data points.
proc print data=LinInterp noobs;
var x y linear;
run;
0.0 |
1 |
1.0 |
0.7 |
. |
2.4 |
1.0 |
3 |
3.0 |
1.3 |
. |
3.3 |
2.0 |
4 |
4.0 |
2.2 |
. |
4.4 |
2.6 |
. |
5.2 |
3.0 |
6 |
6.0 |
3.9 |
. |
9.6 |
4.0 |
10 |
10.0 |
|
Using PROC TRANSREG
In PROC TRANSREG, use the SPLINE transformation or the BSPLINE or PSPLINE variable expansion with the DEGREE=1 option to fit linear piecewise segments. Knots should be specified at the interior, known data points: X=1, 2, and 3 in this example. This is accomplished by specifying the NKNOTS= option with the value equal to the number of known data points minus 2. In this example, there are five known points, so NKNOTS=3 is specified.
proc transreg data=mydata;
model identity(y)=spline(x / degree=1 nknots=3);
output out=LinInterp2 predicted;
run;
proc print data=LinInterp2 noobs;
var x y py;
run;
The results match those from PROC EXPAND. The linearly interpolated values are placed in the variable PY.
0.0 |
1 |
1.0 |
0.7 |
. |
2.4 |
1.0 |
3 |
3.0 |
1.3 |
. |
3.3 |
2.0 |
4 |
4.0 |
2.2 |
. |
4.4 |
2.6 |
. |
5.2 |
3.0 |
6 |
6.0 |
3.9 |
. |
9.6 |
4.0 |
10 |
10.0 |
|
Operating System and Release Information
SAS System | SAS/ETS | All | n/a | |
SAS System | SAS/STAT | All | n/a | |
*
For software releases that are not yet generally available, the Fixed
Release is the software release in which the problem is planned to be
fixed.