Monday, December 19, 2016

Hack 4.5 Determining How Large an Integer You Can Store in a Numeric Variable

SAS Programming Professionals,

Did you know that the CONSTANT function can help you to determine how large an integer you can store in a numeric variable with a given number of bytes? 

This can be helpful when you are working at slimming-down SAS data sets by reducing the number of bytes for numeric variables.  If you know—and know for sure—the largest value that will be stored in that variable, and that you will only be using it to store integers, then you can set its byte-size accordingly.

Here is how you can use the CONSTANT function with the EXACTINT constant:

data _null_;

do i = 2 to 8 by 1;

exactint = constant('EXACTINT',i);

put i exactint;

end;

run;

The PUT statement produces the following:

2 32
3 8192
4 2097152
5 536870912
6 137438953472
7 3.5184372E13
8 9.0071993E15

So, a numeric variable created with a length of 4 will allow me to store integers up to a value of 2,097,152. Note that these results may vary depending on the platform you are using to run SAS.

Two other CONSTANT function constants that are worth exploring are:  BIG and SMALL.

·        BIG = constant(‘BIG’); - Returns the largest double-precision floating-point number (8-bytes) that can be represented on your computer.

·        SMALL = constant(‘SMALL’); - Returns the smallest double-precision floating-point number (8-bytes) that can be represented on your computer.

Check out the CONSTANT function for more insights on how numbers are handled on your computing platform in the online documentation at support.sas.com.  Then, see if it makes sense to slim down some of your observations by right-sizing your numeric variables.

Best of luck in all your SAS endeavors!

 ---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e
Kindle edition: http://tinyurl.com/zypgqa7

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0

Monday, December 12, 2016

It Only Hurts When I Hit ENTER


SAS Programming Professionals,

If you want to take a break from the serious side of programming, then check out my new book:  It Only Hurts When I Hit <ENTER>

My new book is available on Amazon.com as a print edition:  http://tinyurl.com/z8bzx2e
…or a Kindle e-book:  http://tinyurl.com/zypgqa7
Best of luck in all of your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Amazon Author Page:
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0

Monday, December 5, 2016

Hack 4.4 Creating SAS Date Variables with the MDY Function

SAS Programming Professionals,

Did you know that you can create a SAS date variable from a set of month, date, and year variables?

The MDY function creates a SAS date value from month, day, and year values.  It can be helpful when you process data where the unfortunate decision was made to collect month, day, and year in separate variables.  Here  is an example of the MDY function:

data newdate;

format birthdate worddate.;

birth_day = 27;
birth_month = 4;
birth_year = 1975;

birthdate = mdy(birth_month, birth_day, birth_year);

yearsold = year(today()) - year(birthdate);

run;

proc print noobs;
run;

In the example, we employ the MDY function to create a SAS date variable, BIRTHDATE, from separate birth_month, birth_day, and birth_year variables.  Then, we use BIRTHDATE to compute the subject’s age.

A derivative of this simple technique can be used in cases where you get the month and year of a date, but for privacy’s sake, you do not get the day.  You can hardcode a given day value—say 15 because it is the middle of most months—and create a reasonable date that you can use in calculations.  For example, consider the DATA step above without the day variable:

data newdate;

format birthdate worddate.;

birth_month = 4;
birth_year = 1975;

birthdate = mdy(birth_month, 15, birth_year);

yearsold = year(today()) - year(birthdate);

run;

In this example, we hard-coded 15 in the MDY function so that we could compute a valid date for BIRTHDATE.  We could have just as easily used a 1 or a 28 to signify the first day of the month, or a day near the end of the month.  Which is the better way to go?  It is your manager’s sage choice, of course!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0