Haikuo, forget the previous code, from michtka last example everything is much clear. Again, if tables are ordered by subjid,activity and subjid,awake,sleep and there's no overlapping intervals (has shown upside) by simply conditionally iterating through test1 (test2 would be the driver table) you can have the job done. No need for loop iterations. data want (drop=sleep awake _:) ; * drop limits and temp vars; set test2 ; retain group; * iterate through available limits; do while (subjid > _subjid or (subjid = _subjid and (activity > sleep and sleep ne .))); group=_group; * set default group (previous); set test1 (rename=(subjid=_subjid group=_group)); end; if (awake <= activity <= max(sleep,activity)) then group=_group; * set correct group; run; Here's the result running michtka's last sample, handling correctly values that are outside ranges. Obs subjid activity group 1 1 15 1 2 1 25 1 3 1 35 2 4 1 49 2 5 1 58 5 6 2 71 1 7 2 86 2 8 2 90 3 9 2 104 3 10 2 115 2 Cheers from Portugal. Daniel Santos @ www.cgd.pt
... View more