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:
- Creating formatted output to the SAS log from your DATA step (thanks to Amadeus Software for the comprehensive tips)
- More on numeric precision in SAS software
7 Comments
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:
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.
Pingback: Ron Cody is giving me homework - The SAS Bookshelf
Pingback: Pitfalls of the LAG function - The SAS Dummy
Pingback: Myths about 64-bit computing on Windows - The SAS Dummy
where can i find solutions to even number problems in learning sas by example book by Ron cody
I'm not sure, but if some questions have you stumped you can always ask the SAS Support Community (communities.sas.com) for help.