I would approach this a bit differently.
I'd keep the case and control datasets separate and apply the rand_num as you have done.
Drop the SORT and RANK. (BTW, RANK is working correctly. SSN is a unique key and it restarts the ranking every time the by level changes.)
Do the SQL, but change to two datasets and drop the rand_num part of the JOIN. Include the case.ssn with the control.ssn in the output dataset and drop the other case variables.. This will get you ALL of the matches for each case, with duplication, so now your task is to de-duplicate. Non-trivial, but doable.
OR, you can use one of the man 1:N matching macros that people have already written. Google search for
sas matching macro
and you will find a number.
Statistically, a 1:5 match isn't that much more informative than a 1:2 match. Depending on your next steps, you could also use all fo the matches (1:1-1:x) and have a perfectly valid analysis. The use of a limited number of matches is valuable if you have to do manual abstraction (e.g. extra labor), but if you are totally working with computer files, what's a few extra cycles to use them all.
... View more