Next time, do your own homework

SAS programming is taught in schools all over the world, including in high schools.  Occasionally, I receive questions via my blog such as this one:

Can somebody help me on this?
Write a short DATA _NULL_ step to determine the largest integer you can store on your computer in 3, 4, 5, 6, and 7 bytes.

This sounds like a homework assignment to me, not a typical "how do I" programming question from a person trying to get a job done.  I'm pretty sure that the professor who assigned it would not sanction a lazy web approach.

Besides that, given the brief problem statement above, how do we know what the professor wants?  Is the student supposed to write a program to calculate the largest integer that you can safely store within SAS?  Or is it okay to be clever and simply use built-in SAS functions to tell you the answer?  The first approach is an exercise in programming logic and math.  The second approach is a test of your resourcefulness -- can you find the answer without a "brute force" approach, perhaps by becoming familiar with SAS documentation?  Both approaches are valid, and will provide you with practice in skills that will help you with your ongoing SAS programming endeavors.

At the risk of rewarding lazy behavior, I will present one method to find the answer.  I'm sharing it only because it sheds light on the useful CONSTANT function, and other readers might find that helpful in their own work.

In this example, we'll use the EXACTINT constant. From the SAS documentation:

The exact integer is the largest integer k such that all integers less than or equal to k in absolute value have an exact representation in a SAS numeric variable of length nbytes. This information can be useful to know before you trim a SAS numeric variable from the default 8 bytes of storage to a lower number of bytes to save storage.

Here's the program:

data _null_;
  array len{8} _numeric_ BYTES_1-BYTES_8;
  do i=3 to 8;
   len{i} = constant('exactint',i);
  end;
  pi = constant('pi');
  put "Decimal:" (BYTES_3-BYTES_8) (=/comma32.0);
  put "Hexidecimal:" (BYTES_3-BYTES_8) (=/hex14.);
  put "Binary:" (BYTES_3-BYTES_8) (=/binary64.);
  put "and a slice of Pi:" (pi) (=/16.14);
run;

You'll have to run the program in SAS to see the results. As my high school history teacher used to say, "I'm not going to spoon-feed the answers to you." (Although I suppose that's what I just did...)

Bonus (even though you don't deserve it)

Here are a couple of resources that may be helpful in future assignments:

tags: CONSTANT function, homework, SAS functions, SAS programming

2 Comments

  1. Posted September 22, 2011 at 12:13 pm | Permalink

    Dear Students,
    Remember, you are never to young to start thinking about matrices and vectors. Your teacher will give you bonus points if you submit this SAS/IML code:

    proc iml;
    Bytes = T(3:8);
    LargestInt = constant("ExactInt",Bytes);
    print Bytes LargestInt[format=comma32.];
    
  2. Chris Hemedinger Chris Hemedinger
    Posted September 22, 2011 at 2:16 pm | Permalink

    The plot thickens: a web search on the phrasing from the question found one hit: right in the middle of Ron Cody's book Learn SAS Programming by Example. It's an exercise from one of the chapters. The answers to odd-numbered problems are available for anyone to download from the support.sas.com/cody web site.

    But this question is from an even-numbered problem! I was able to sneak around over here and locate the intended solution though, from a file usually supplied only to professors by request.

    So was my solution similar to the one that Ron Cody supplied? I'm not telling.

3 Trackbacks

  1. By Ron Cody is giving me homework - The SAS Bookshelf on September 30, 2011 at 3:38 pm

    [...] homework pleas with a public answer, but on this day I felt up for a challenge, and I decided to answer the post on my blog and perhaps embarrass the student who was too lazy to complete the exercise [...]

  2. By Pitfalls of the LAG function - The SAS Dummy on January 3, 2012 at 2:25 pm

    [...] least, I'm afraid that I did. I think I might have helped a SAS student with a homework assignment, or perhaps provided an answer in preparation for a SAS certification exam. Or maybe it was a [...]

  3. [...] POP QUIZ: What's the largest integer that SAS can store in 8 bytes with exact representation? [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>