SAS – Custom Statistics from Merge Operations

The code below shows how to get custom statistics from merge operations above and beyond what the standard DATA step output provides.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    <http://www.gnu.org/licenses/>.
    Developed by Mario Segal

data c;
merge a (in=left) b (in=right) end=eof;
retain missleft missright;
by merge_key;
if left then output; *We want all records of left, modify as needed;
*collect the desired statistics;
if left and not right then missright+1;
if right and not left then missleft+1;
if eof then do; *output the statistics;
put ‘There were ‘ missleft ‘Record on left dataset not found on the right dataset’;
put ‘WARNING: There were ‘ missright ‘ Records on right dataset not found on the left dataset’;
end;
drop miss: ; *don’t forget to drop the statistci variables or they will be part of the output dataset;
run;

Leave a comment