Many thanks for the very useful responses in such a short time. I'm not a former SAS user - more a naive R user who didn't realise that a sort wasn't necessary! Jim, your solution worked really well - thanks. Thanks again for the great solutions. Steve