Rick I took your challenge to have a go, and have come up with a double CUSUM approach. The idea is to generate the vector {1 0 1 1 0 0 1 0 0 0} that when CUSUMed, becomes the list of elements from x (with repeats) that you want to see. It's not going to handle the missing values and zeros, so if necessary, you would need to add your own code above to preprocess the input vectors.

proc iml;
start expfr(x,f);
return(x[cusum(element(1:f[+],cusum(1//f)))]);
finish;

values={A,B,C,E};
freq = {2,1,3,4};
print (expfr(values,freq));
quit;