Hello Team, I wondered if users had any experience with "multiple conditional" LEFT JOINs in a PROC SQL? What I currently have is a PROC SQL with a LEFT JOIN operation that retrieves the variable reference based on id, date, and loc1, which you can see in the code below (I am using SAS 9.4.): proc sql;
create table table1 as
select a.*,b.reference
from event1 as a
left join master1 as b on a.id = b.id and a.date between b.date1 and b.date2 and a.loc1=b.loc1 and a.loc1^=.;
/* left join master1 as b on a.id = b.id and a.date between b.date1 and b.date2 and a.loc1=b.loc2 and a.loc1^=.;*/
/* left join master1 as b on a.id = b.id and a.date between b.date1 and b.date2 and a.loc3=b.loc3 and a.loc3^=.;*/
quit; What I would like to do (in the same PROC SQL statement) is to have options for alternative locations (as id and date cannot change in my context) when there are no matching records in the right table (i.e., master1). For example, if there are no records for some rows, I would like to retrieve matches on a.loc1 = b.loc2, and if there is still not a match, then to retrieve matches based on a.loc3 = b.loc3 (see the comments in the SAS code above for the two additional LEFT JOIN operations that I would like to incorporate - in same way - in the PROC SQL). However, I would need to instruct SAS to use the second (or third) LEFT JOIN only for the rows where the current LEFT JOIN operation found no matching records in the right table. So when the first LEFT JOIN finds no matching records for some (or all) rows, move on to the second one, and finally to the third one, assuming the second one has some no matching records either. Additionally, it would be great if the SAS code can create three additional variables (i.e., additional to the variable reference) where I can tell which LEFT JOIN operation the SAS code used for the matching record, say a dummy variable equal to 1 for var1 (when loc1 was used, zero otherwise), equal to 1 for var2 (when loc2 was used, zero otherwise), and equal to 1 for var3 (when loc3 was used, zero otherwise). I would appreciate your help on this one and any advice (or example) as to how I can adjust the code in the above example, please. Thank you
... View more