I suspect you can solve the new problem if you give it some effort.
There are two "edge cases" that you need to worry about because there are no "adjacent values" to average:
What happens if an element of A is less than all elements of B? Is the result missing? Or maybe the 1st element of the Other vector?
What happens if an element of A is greater all elements of B? Is the result missing? Or maybe the last element of the Other vector?
In the program below, I've coded one way to handle these edge cases, but you might want to ask your client what they prefer.
proc iml;
A = {10, 3, 19, 0, 45};
B= {1, 3, 7, 42}; /* assume B is sorted in increasing order */
Other = {100, 200, 300, 400, 500}; /* assume Other is same size as B */
nB = nrow(B);
nA = nrow(A);
Result = j(nA, 1, .);
do i = 1 to nA;
idx = loc(B = A[i]);
if ncol(idx) > 0 then do; /* did we find an exact match? */
Result[i] = Other[idx]; /* if so, index into the Other vec */
end;
else do; /* not an exact match; average adjacent vals */
idx = loc(B <= A[i]);
/* 3 cases: EMPTY, the last index, or valid index that is not last */
if ncol(idx)=0 then /* A[i] < all elements of B */
Result[i] = .; /* or you could use Other[1] instead */
else do;
if all(idx < nB) then do; /* average the Other elements */
j = idx[ ncol(idx) ];
Result[i] = (Other[j] + Other[j+1]) / 2;
end;
else
Result[i] = Other[nB]; /* or missing? */
end;
end;
end;
print A Result;
... View more