Yes, I'm sure it can be done in SQL, but I personally refuse to accept the limitation that it must be in SQL, which then prevents me from using the power of SAS and prevents me from using all the features of SAS. In addition to the reasons I gave for doing this on a long data set and using PROC SUMMARY, if you have code that works for 4 values of x, and then at some point in the future you have more than 4 values of x (like 5 or 6 or 12 or 107 values of x), you have to modify the SQL code, whereas in my code no modification is necessary, even if you have 107 values of x. If you have a lot more than 4 values of x, this modification of SQL would become tedious and error-prone. So, I will stick with my answer. I'm sure someone will come along and provide SQL code to do this. For anyone else reading along, don't do this in SQL, don't make it harder than it needs to be.
The code I gave works if the weights change for every value of i.
... View more