UNION Operator keywords Displays all rows from both the tables Removes duplicate records from the combined dataset keywords ALL -- duplicate rows in the combined dataset. CORR -- PROC SQL matches the columns by name.
Remove nonmatching columns. UNION Operator UNION CORR ALL Remove duplicate rows. No Yes End Concatenate tables. Remove nonmatching columns.
data t1(drop=i) t2(drop=i rename=(z=w)); call streaminit(13453); do i= 1 to 3; x=int(rand("uniform")*5); z=int(rand("uniform")*5); output t1; output t2; end; do i= 4 to 6; run; data t1; set t1 end=done; output; if done then output; data t2; set t2 end=done; if _n_=1 then output; title "t1"; proc print data=t1 noobs;run; title "t2"; proc print data=t2 noobs;run; title;
/*union*/ proc sql; select * from t1 union from t2 ; quit;
/*reverse order of union*/ proc sql; select * from t2 union from t1 ; quit;
/*create a table from union*/ proc sql; create table tot1 as select * from t1 union from t2 ; quit; proc print data=tot1;run;
/*the corr option*/ proc sql; select * from t2 union corr from t1 ; quit;
/*the all option*/ proc sql; select * from t2 union all from t1 ; quit;
/*both corr and all*/ proc sql; select * from t2 union corr all from t1 ; quit;
Payroll Report for Level I, II, and III Employees Create a payroll report for the Level I, II, and III Orion Star employees. The orion.Staff table contains the job title and salary information for all Orion Star employees. Use the UNION set operator to combine the results from each query that calculates the total paid to all Level I, II, and III employees. Payroll Report for Level I, II, and III Employees _______________________________________ Total Paid to ALL Level I Staff 1,234,567 Total Paid to ALL Level II Staff 1,456,789 Total Paid to ALL Level III Staff 2,123,456
The UNION Operator proc sql; select 'Total Paid to ALL Level I Staff', sum(Salary) format=comma12. from orion.Staff where scan(Job_Title,-1, ' ')='I' union select 'Total Paid to ALL Level II Staff', where scan(Job_Title,-1,' ')='II' select 'Total Paid to ALL Level III Staff', where scan(Job_Title,-1,' ')='III'; quit; s106d11
Remove nonmatching columns. UNION Operator UNION CORR ALL Remove duplicate rows. No Yes End Concatenate tables. Remove nonmatching columns.