Hello,
I am trying to apply a triangular kernel weight for a regression discontinuity design for a given bandwidth.
To Illustrate what I want to accomplish, I generated a fake data set and wrote a macro.
However, I was wondering if there was any canned procedure in SAS to accomplish the same thing.
Thanks for the help.
Sincerely,
Bill
DATA TEST;
DO I = 1 TO 1000;
X = RAND("NORMAL", 100, 15);
IF X GT 115 THEN DO;
Y = X + 15 + RAND("NORMAL", 0 , 3);
Z = 1;
END;
ELSE DO;
Y = X + RAND("NORMAL", 0 , 3);
Z = 0;
END;
OUTPUT;
END;
RUN;
PROC SGPLOT DATA = TEST;
SCATTER X=X Y=Y / GROUP = Z;
LINEPARM X=0 Y=0 SLOPE=1 / LINEATTRS=(COLOR = BLUE);
LINEPARM X=0 Y=15 SLOPE=1 / LINEATTRS=(COLOR = RED);
REFLINE 115 / AXIS=X LINEATTRS=(PATTERN = DASH);
XAXIS MIN = 50;
YAXIS MIN = 40;
RUN;
%MACRO KERNEL_WT(DATA, X, CENTER, BW);
DATA WTD_DATA;
SET &DATA;
IF (&X GT &CENTER - &BW) AND (&X LT &CENTER + &BW) THEN DO;
KERNEL = (1 - ABS( (&X - &CENTER) / &BW)) ;
END;
ELSE KERNEL = 0;
RUN;
PROC SQL NOPRINT;
SELECT SUM(KERNEL) INTO :SUM_KERNEL
FROM WTD_DATA;
QUIT;
DATA WTD_DATA;
SET WTD_DATA;
WEIGHT = KERNEL / &SUM_KERNEL;
RUN;
%MEND KERNEL_WT;
%KERNEL_WT(TEST, X, 115, 10);
PROC SURVEYREG DATA = WTD_DATA;
MODEL Y = X Z / SOLUTION;
WEIGHT WEIGHT;
RUN;
Two observations:
%MACRO KERNEL_WT(data,x,center,bw);
data wtd_data;
set &data (in=firstpass)
&data (in=secondpass);
if -&bw < (&x-¢er) < &bw then kernel = (1 - abs( (&x - ¢er) / &bw)) ;
else kernel=0;
if firstpass=1 then sum_kernel+kernel;
if secondpass=1;
weight=kernel/ sum_kernel;
run;
%MEND KERNEL_WT;
Note the SET statement reads the dataset twice. The first time (firstpass=1) to generate SUM_KERNEL, and the second time to generate the weights and output the results.
It looks like you want to specify the location of the discontinuity (CENTER) and the scale (BW) and manufacture a triangular weight function. If so, the answer is no, there is no built-in function. As shown, it takes two lines in the DATA step to implement the transformation.
SAS does support using triangular kernels to estimate the density of a distribution. See the K=TRIANGULAR option on the HISTOGRAM statement of PROC UNIVARIATE.
What are you trying to accomplish with this kernel weight? For example, if you are trying to model data that have a jump discontinuity, we might be able to suggest better ways,
I see. You are trying to perform "kernel regression", which is a form of local regression.
Your current implementation of the technique will only give you the local regression at a single location.
See the article "Kernel regression in SAS" for an implementation. However, a better method is to use loess regression ,
which is available by using PROC LOESS. Here is a loess fit for your data:
proc sgplot data=TEST;
loess X=X Y=Y;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.