next up previous
Next: Merging macros Up: Macros Previous: Subsetting observations from large


Writing frequently-used code as macros

Several operations are so standard that if you write them up as macros and save them in a macro file, you will be spared a lot of typing. You can then automatically include this macro file in all your SAS programs - see the section on autoexec.sas, Section 15.9.

An example is the merge algorithm. Merging always proceeds in the same way. You are given two datasets and the by-variables, you sort them by the by-variables and then merge them. I wrote up a ``standard merge'' macro named %stdmerge:

%macro stdmerge(inds1, inds2, byvar, outds);
    proc sort data=&inds1 nothreads;
       by &byvar;
    run;

    proc sort data=&inds2 nothreads;
       by &byvar;
    run;    
    data &outds;
       merge &inds1(in=a) &inds2(in=b);
       by &byvar;
       if a and b;
    run;
%mend;
   

In the SAS programs I write, when I want to merge two datasets, all I need to do is say

    %stdmerge(firstdataset, seconddataset, by-variable-list, outputdataset)
   

instead of laboriously typing out the whole thing every time. This clears up an enormous amount of clutter from my SAS programs, making them much more readable.



Subsections

Andre de Souza 2012-11-19