While the SAS DATA Step is incredibly powerful for sequential row processing, PROC SQL gives you expressive relational power, compact aggregation, and seamless macro variable integration.
1. Creating Macro Variables with INTO: Separated By
Instead of hardcoding values or writing DO-loops, you can populate a comma-delimited macro variable in a single query:
proc sql noprint;
select quote(strip(name))
into :subject_list separated by ', '
from sashelp.class
where age >= 14;
quit;
%put Selected Students: &subject_list;
2. Calculating Summaries and Re-joining Automatically
In standard SQL, calculating group percentages requires a subquery or window function. SAS PROC SQL detects aggregate functions combined with non-grouped variables and automatically re-merges the summary data back onto each row!
3. The Coalesce Function for Merges
When performing FULL JOIN operations where keys exist in both tables, coalesce() ensures you never drop subject IDs:
proc sql;
create table merged_data as
select coalesce(a.id, b.id) as ID,
a.var1,
b.var2
from dataset_a as a
full join dataset_b as b
on a.id = b.id;
quit;