The first table contains Key-value & time like below
Second Table contains each IDs have its start and end date.
I'd like to find out ID for each row from time_stamp.
There are fixed numbers of Categories. but there are a lot of ID.
Would you help me how to write a SQL query? (Any SQL Style is fine. I can convert that. PROC SQL in SAS compatible would be better)
If you're doing this in SAS, you're better off using a format. Formats have the advantage of taking start/end ranges, and are REALLY fast - something around o(1) time if I recall correctly. This doesn't require sorting the larger dataset (and even could avoid sorting the smaller dataset if that was an issue), which most of the SQL solutions probably will unless they can hold the smaller dataset in memory (as a hash table).
The first two data steps just create your data above, the
format_two
data step is the first one that does anything new.If there are more categories, as long as they're alpha and not numeric this should still work fine; the only difference you'd want to change is the
if _n_ le 2
should have the 2 equal (total number of categories).