SAS PROC FORMAT - Define 'Other' Range for Numeric Variable

307 views Asked by At

I've created a format that assigns a State label to a pre-defined range of Start-End numeric postcodes.

i.e. 
Start | End | State
2600 | 2618 | ACT
2900 | 2949 | ACT
4000 | 4899 | QLD

I want to add a piece of code to the format file that assigns it a label "Error" when the postcode falls outside of my range.

In a previous thread, someone suggested using the HLO solution but I have tried implementing it with mixed success.

rsubmit;
    data Fmt_State;
        set State end=eof;
        retain type 'n';
        fmtname = 'category';
        start = pcode_fr;
        end = pcode_to;
        label = State;

        * For the first observation, assign the ‘LOW’ keyword; 
        if _n_ eq 1 then HLO='L';
            if eof then do;
                *** For the last observation, assign the ‘HIGH’ keyword and output;
                HLO='H';
                output;

                *Define an 'Other' range that is outside the bounds of LOW and HIGH; 

                HLO='O';
                label = "Error";
                output;
            end;
        else output;
    run;
endrsubmit;

Oddly, only the Middling ranges BETWEEN Low-High that are correctly labelled Error and the ranges outside Low-High that are incorrectly labelled. (I would expect the opposite to be true but still not working the way I want)

For clarity this is what's happening from my same ruleset:

Pcode | ShouldReturn (Reason) | ActuallyReturns
2615 | ACT | ACT
2000 | Error (TooLow) | ACT
2700 | Error (Undefined range) | Error
5000 | Error (Too High) | QLD

I just want anything undefined to be called Error despite it being too low or too high. Please help!

1

There are 1 answers

3
Tom On BEST ANSWER

Your logic is incomplete. You want to add three new records.

START END  HLO LABEL
   .  2600 L   Error (Too Low)
4899     . H   Error (Too High)
   -     - O   Error (Undefined range)

So you want logic like this:

if _n_=1 then do;
 start=.;
 end=from;
 hlo='L';
 label='Error (Too Low)';
 output;
end;
start=from;
end=to;
label=state;
hlo=' ';
output;
if eof then do;
 end=.;
 start = to;
 hlo='H';
 label='Error (Too High)';
 output;
 hlo='O';
 label='Error (Undefined Range)';
 output;
end;