A Fast Format Macro – How to Quickly Create a Format by Specifying the Endpoints

From sasCommunity
Jump to: navigation, search
Christine A. Smiley

Kestnbaum, a KnowledgeBase Marketing Company

Chicago, IL

Abstract

Have you ever needed to create a format for a variable that required a large number of ranges? If so, you have probably discovered how tedious the use of PROC FORMAT can become. This paper illustrates a macro that can create a numeric format easily and quickly using SAS® software. The method used by the macro and the logic behind it are explained. The CNTLIN option in PROC FORMAT is described.

The makeform macro code

%macro makeform(name, endpoint);

data macdata;
    length start $ 8 end $ 8 label $ 20 ;
    fmtname = "&name";

    retain i;
    i = 1;

    do while
    (scan("&endpoint", i, " ") ne ' ');

        end = scan("&endpoint", i, " ");

        start = lag(end);
        if start = ' ' then start = 'low';

        label = left ( trim(start) ||
                '< to ' || left(end) );
        if start=’low’ then
            label = left ( trim(start) ||
                    ' to ' || left(end) );

        output;
        i = i +1;
    end;
    drop i;

proc format cntlin=macdata;

%mend;

Advanced version of the makeform macro

%macro makeform(name, endpoint);
options nonotes;

data _null_;
    put ' ';
    put 'proc format;';
    put "value &name";

data macdata;
    length start $ 8 end $ 8 label $ 20 ;
    fmtname = "&name";
    retain i;
    i = 1;

    do while (scan("&endpoint", i, " ") ne ' ');
        end = scan("&endpoint", i, " ");
        start = lag(end);
        if start = ' ' then start = 'low';
        label = '>'||left(trim(start)||' to '||left(end));
        if start='low' then
            label = left(trim(start)||' to '||left(end));
        output;
        if start='low' then put start '- ' end '= ' ''label'';
        else put start '< - ' end '= ' ''label'';
        i = i +1;
    end;

    drop i;

data _null_;
    put ';';
    put 'Specify data set name in all procedures below.';

proc format cntlin=macdata;
options notes;
%mend;

Online materials