Google SAS Search

Add to Google

Wednesday, November 15, 2006

Creating Numeric Buckets

The other day I was writing some code that was needed for a report. Part of the report was to take a number (integer) and fit it into a set of "buckets" at intervals of 100, rounded up. Confused? Here's some examples of what I needed:
3 --> 100
101 --> 200
1536 --> 1600
64 --> 100


Here's the line of code I used to accomplish it:
newNumber = ( ceil( myNumber/100 ) ) * 100;

Certainly not the most cerebral code ever written, but (hopefully) worth sharing.

This type of problem (creating numeric buckets) is fairly common and I was wondering if anyone else had a different way of solving it?

3 comments:

  1. I always use formats for such a thing. The buckets vary too much to have a consistent mathematical formula.

    Alan Churchill

    ReplyDelete
  2. round function also works but this assumes that your original var only has integers.

    data _null_;
    do var = 0, 3, 100, 101, 1536, 64;
    bucketed = round(var + 49.9, 100);
    put (_all_) (/=);
    end;
    run;

    ReplyDelete
  3. Alan,
    I have to admit, formats were my first inclination; however, I couldn't think of a way of implementing it without a bunch of dance around code (ie, find the max value, loop by 100 to create the buckets, etc). Maybe you had a better way? Picture formats?

    As always, thanks for commenting!

    ReplyDelete