Chapter 2 Supplement Correlated Subqueries © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina 1 Example – Correlated Subquery AtBats Playerposition Player Atbats Player Position Walker 271 Marzilli Outfield Wingo 240 Williams Outfield Thomas 231 Bradley Jr. Outfield Marzilli 220 Wingo Infield Beary 211 Walker Infield Morales 249 Thomas Infield Mooney 254 Marzilli Infield Williams 209 Morales Infield Bradley Jr. 162 Mooney Infield 2 Correlated Subqueries In Chapter 2, we saw one example of a correlated subquery: proc sql; select player, atbats from atbats where "Infield"= (select position from playerposition where atbats.player=playerposition.playe r); quit; 3 Example Correlated Subquery Step 1 – The outer query takes the first row in atbats table and finds the columns player and atbats. Step 2 – Match atbats.player (passed from table in outer query) with playerposition.player to find the qualifying row in the playerposition table. Step 3 – The inner query now passes the position of the selected row in playerposition back to the outer query via the = operator, where the position is matched for the selection in the outer query. 4 Example – Correlated Subquery Player Atbats Walker 271 Wingo 240 Thomas 231 Beary 211 Morales 249 Mooney 254 5 Example-Correlated Subquery Correlated subqueries suggest that SAS has the ability to resolve ambiguous references. Regardless, when creating code, we often get error messages that look like: ERROR: Unresolved reference to table/correlation name varname 6 Example-Correlated Subquery We can modify the correlated subquery so that - The reference to playerposition.player is less clear - playerposition.player is not even selected in the outer query, but does appear in the referenced data set (atbats) 7
© Copyright 2026 Paperzz