SAS (Base SAS)+
data SEX;
infile datalines dlm='|' dsd missover;
input Name : $8. Sex : $1.;
label ;
format ;
datalines4;
Alfred|M
Henry|M
Mary|F
;;;;
run;
data AHW;
infile datalines dlm='|' dsd missover;
input Name : $8. Age : best32. Height : best32. Weight : best32.;
label ;
format ;
datalines4;
Alfred|14|69|112.5
Henry|14|63.5|102.5
James|12|57.3|83
;;;;
run;
*------------------------------------------------------------------------------;
*full join;
*------------------------------------------------------------------------------;
data full;
merge sex ahw;
by name;
run;
*------------------------------------------------------------------------------;
*left join;
*------------------------------------------------------------------------------;
data left;
merge sex(in=a) ahw;
by name;
if a ;
run;
*------------------------------------------------------------------------------;
*right join;
*------------------------------------------------------------------------------;
data right;
merge sex ahw(in=b);
by name;
if b ;
run;
*------------------------------------------------------------------------------;
*inner join;
*------------------------------------------------------------------------------;
data inner;
merge sex(in=a) ahw(in=b);
by name;
if a and b;
run;
infile datalines dlm='|' dsd missover;
input Name : $8. Sex : $1.;
label ;
format ;
datalines4;
Alfred|M
Henry|M
Mary|F
;;;;
run;
data AHW;
infile datalines dlm='|' dsd missover;
input Name : $8. Age : best32. Height : best32. Weight : best32.;
label ;
format ;
datalines4;
Alfred|14|69|112.5
Henry|14|63.5|102.5
James|12|57.3|83
;;;;
run;
*------------------------------------------------------------------------------;
*full join;
*------------------------------------------------------------------------------;
data full;
merge sex ahw;
by name;
run;
*------------------------------------------------------------------------------;
*left join;
*------------------------------------------------------------------------------;
data left;
merge sex(in=a) ahw;
by name;
if a ;
run;
*------------------------------------------------------------------------------;
*right join;
*------------------------------------------------------------------------------;
data right;
merge sex ahw(in=b);
by name;
if b ;
run;
*------------------------------------------------------------------------------;
*inner join;
*------------------------------------------------------------------------------;
data inner;
merge sex(in=a) ahw(in=b);
by name;
if a and b;
run;
- To merge two datasets in SAS, we sort each one by the key variable and then use a DATA step with
MERGE+BY. proc sort data=class; by Name; run;sorts each input on the merge key — SAS requires sorted input for a BY merge.- In the DATA step,
merge class class2; by Name;interleaves rows by matching Name values across the inputs. - For matched Name values, SAS writes one combined row with columns from both sides. For Name values in only one input, SAS still writes a row but with missing values for the absent side’s columns (the equivalent of a full outer join).
- The default SAS merge is full-outer. To mimic an inner join, we add
if in_a and in_b;using thein=dataset options to mark membership.