SIGNIF and GENFMT, SASe Macros for Computing and Reporting Significant Digits B. M. Horwedel, Oak Ridge National Laboratory' J. E. Horwedel, Oak Ridge National Laboratory' Abstract excess digtts as a decimal fraction; Reporting generalized statistics such as mean or standard error with excessive significant digits can be misleading and imply more precision than that fOlind in the original measurements. Measurement data seldom warrant more than two or three significant figures to represent values. An appropriate number of digHs is determined by the precision of the reported value. (2) Kthe fraction is greater than 1/2, increment the least significant digit; (3) K the fraction is less than 1/2, do not increment; (4) If the fraction equals 1/2. increment the least significant digH only ff H is odd. This poster presents two SAS macros that are useful in reporting values with a IimHed number of signfficant digHs. SIGN IF is a macro that will return a value rounded to a specffied number of digHs. GENFMT is a macro that will generate a picture format to be used in printing numbers. Examples using SIGNIF and GENFMT are shown. The reason for rule (4) is that in many cases the fraction equals etther 0 or 1/2 and conSistently incrementing the least signfficant digH for a fraction of 1/2 would lead to a systematiC error. Discussion The pseudocode for rounding a number to n significant digHs is as follows: Precision is communicated by the way in which a measurement is reported. MOVE selected number of significant digits to Statistics reported with more significant digits than the number of digHs found In the original measurements are misleading. I~ of decimal pOint. The number of significant figures refers to the number of numerical digits used to express a value. Set VALUE equal to truncated decimal. If fractional part is GREATER than 0.5 increment VALUE Computations on a computer can be so simple that the final resu~ of a sequence of calculations will appear more precise than tt really is. In most statistical work, H is best to carry more figures into the final computation than seem necessary and then to round the result to a meaningful number of digits, relative to the precision of the original measurements. An appropriate number of digHs is that which is justified by the precision of the measurement. The number of significant figures in a result is defined as: Else, if fractional part EQUALS 0.5 then ff VALUE is ODD Increment VALUE SIGNIF is used like any other SAS macro. Three arguments must be specified. The first argument is the variable to store- the result; the second argument is the 1. The leftmost nonzero digH is the most significant digH. 2. If there is no decimal pOint, the rightmost nonzero digit is the least significant digtt. 3. If there is a decimal point, the rightmost digH is the least signfficant digit, even ff H is O. 4. All digits between the least and most significant digHs are counted as significant digits. value to be rounded; and the third argument specifies the Consider the following number of signfficant digHs. example: x = 8.234; &SIGNIF(y,X,2); Y will be set to the value 8.2. The rules for rounding numbers to a smaller number of significant digits can be found in several references. [1] The SAS function ROUND does provide some of the same capabilities as SIGN IF; however, ROUND is more limited. The ROUND function rounds a value to the nearest (1) Truncate the number to n digits. and treat the 1306 picture format generated by GENFMT will provide this control. roundoff unn which must be supplied by the user. It is difficutt to specify one roundoff unn for data wnh a large range. SIGN IF is designed to work wnh a large range of numbers. Restrictions and Umitations SIGNIF and GENFMT were implemented and tested on a VAXNMS computer under SAS version 6.06. They should be tranportable to any system that supports the MACRO language. GENFMT is a macro that will provide a picture format to be used in reporting values with a specified number of digits. GENFMT needs to be called once before using the format. For example, to print numbers wnh two signnicant digns: Condusion &GENFMT(2); SIGNIF is a macro callable from a SAS program to round a value to a specnied number of digns. SIGN IF allows reporting statistics on - measurement data limited to the number of significant digits warranted by the precision of the originaJ measurements without the limitations imposed by the SAS ROUND function. GENFMT generates a picture format to control the printing of values with a specnied number of digtts. GENFMT will generate a picture format, DIG2X. Use this format as any other SAS format. See Reference 2. An example of the use of SIGN IF and GENFMT in a SAS program follows: DATA TEST; INPUT X; &SIGNIF(y,X,2); '* y is x rounded to two digns*, References YY~Y; RUN; &GENFMT(2); '* generate a picture formatO, RUN; PROC PRINT; VAR X YYY; FORMAT X Y BEST10. YY DIG2X.; '* yy is printed wnh two significant figures *' [11 BeVington, P.R., Data Reduction and Error Analysis for the Physical SCiences. McGraw·HiII Book Company, 1969. [21 SAS Instnute, Inc., SAS Language: Reference, Version 6, First Edition. Cary, NC: SAS Institute, Inc., 1990. 'Managed by Martin Marietta Energy Systems, Inc., under contract DE·AC05·840R21400 wnh the U.S. Department of Energy. The variable Y is defined as X rounded to two significant digits; YY is then set equal to Y. The variables X and Yare printed wnh the BEST format; YY is printed wnh DIG2X. The printout from the above program is shown: x y 0 51 1.75 1.85 99.5 1254 ·51 ·1.03 0.4451 0.0103 0.00185 0 51 1.8 1.8 100 1300 ·51 ·1 0.45 0.01 0.0018 "VMS is a trademark of Dignal Equipment Corporation, Maynard, MA. yy 0 51 1.8 1.8 100 1300 ·51 ·1.0 0.45 0.010 0.0018 Five picture formats are included in Appendix C. These picture formats have been set up as separate files; GENFMT will 'INCLUDE' the appropriate format. These formats can be easily modnied to conform to your reporting requirements (e.g., decimal alignment, ranges, etc.). In all cases, the number 0 is printed as the dign 0 wnh no decimal. Before using these picture formats, numbers must be rounded; picture formats truncate. The SAS BEST format will in most cases print a number with the proper number of signnicant digns (after tt has been rounded); however, there is no control in lining up the numbers. The 1307 Appendix A. Usting of SIGNIF.SAS. %MACRO SIGNIF(value,inH_val,sig); /*************************** •• ***.********.* = VALUEXX &intt val; - ~LVALUEXXNE-O) then do; ~ &intt_val < 0 VALUEXX; then VALUEXX fACTORX 10**(&SIG.ceil( logI0LVALUEXX))); VALUEXX VALUEXX * FACTORX; =INNALX iriiLVALUEXX); IFLVALUEXX - JNNALX > 0.5) INNALX + 1; THEN INNALX ELSE - Macro: SIGNIF (value, Intt_val, sig) Purpose: SIGNIF returns the first argument as the second argument ·rounded~ to the number of significant digtts specified by the third argument. = = = Authors: B. M. Horwedel, J. E. Horwedel = Computing and Telecommunications Division Martin Marietta Energy Systems Oak Ridge National Laboratory Building 6011, MS 6370 P.O. Box 2008 Oak Ridge, Tn. 3783t -6370 = 0.5) IF LVALUEXX - JNNALX THEN IF (modUNNALX,2) 1) THEN INNALX INNALX + 1; &VALUE INiVAlX/ FACTORX; ~ &inH_val 0 then &VALUE -&VALUE; end; = <" = Phone number: (615)-574-5413 (FTS)-624·5413 else &value = 0; DROP _FACTORX_INNALX; %MEND; Created: December 1, 1990 Arguments: value: intt_val: sig: =- rounded result value to be rounded required number of significant digits; Notes: SIGN IF is a SAS macro that is used in a SAS program to return a value wtth a spec~ied number of significant digtts. The method is to truncate the number to the spec~ied number of digits. Treat the excess as a fraction. If the fraction is greater than 0.5, increment the number. If the fraction is equal to 0.5, increment the number only ~ it is odd. For further discussion see Reference 1. Restrictions: Version 6 SAS must be available on the Vf>J(NMS for implementation of SIGN IF. Pseudo code: Determine factor to move sig digtts to left of decimal. Use factor to scale up (or down). Get integral value and fractional part. If fractional part is greater than 0.5 increment value; else ~ fractional part equals .5 increment if value is Odd. Scale down (or up) with factor. return value. ***************************************************/ 1308 = = Appendix B. Usting of GENFMT.SAS. *DIG4.SAS: proc format: picture DIG4X %MACRO GENFMT(dig); %include dig&dig; %mend; -0.9999--0.1 -9.999--1.0 -99.99--10.0 -999.9--100 -9999- -1 000 run; o Appendix C. Ustings of DIG1.SAS - DIG5.SAS. 0.1-0.9999 1.0-9.999 10.0-99.99 100-999.9 1000-99999 *DIG1.SASj proc format: picture DIG1X ,0.00099,,0.0001 '0.0099"0.001 -0.099--0.01 -0.99--0.1 -9999--1.0 = , =' 9.9999' 9.999 I 9.99 9.9 . = I o I 0009 = I 9 0.0001-0.00099 0.001-0.0099 0.01-0.099 0.1-0.99 1.0-99999 =' I (prefix="') (prefix='-') (prefix='-') (prefix='-') o = 0.001-0.0099 0.01-0.099 0.1-0.99 1.0-9.9 10.0-99999 I 0099 I 9 o 1.0-9.9 10.0-99.9 1000-9999 10000-99999 (prefix:'-') (prefix='-') (prefix='-') 9.9999' 9.999 9.99 9.9 '00099 *DIG3.SAS; proc format: picture DIG3X -0.0999--0.01 -0.999--0.1 -9.99--1.0 -99.9--10 -9999--100 = = = = I I I I 0999 o 1.0-9.99 10.0-99.9 100-99999 (prefix='-') (prefix='-') (prefix='-') (prefix='-') (prefix='- I) 9 9.9999' 0.01-0.0999 0.1-0.999 9.9999' 9.999 I 9.99 I 99.9 I = = I 9.9999' = , 9.999 99.99 999.9 '09999 picture DIG5X -9.9--1.0 -999--100 -9999- -1 000 (prefix;;'-') I (prefix='-') (prefix=IMI) (prefix='M') proc format: 9.9999 1 (prefix='-') 9.999 9.99 9.9 (prefix="') (prefix='o') *DIG5.SAS; 9.9999' 9.999 9.99 = I 9.9 '00009 =, =, 9.9999' 9.999 99.99 f 999.9 9999 9 (prefix='" ) *DIG2.SAS; proc format; picture DIG2X -0.0099--0.001 -0.099--0.01 -0.99--0.1 -9.9--1.0 -9999--10.0 =, =, 9.999 9.99 99.9 '00999 I 1309 9.9999' (prefix='_') (prefix='-') , 9999.9 (prefix=IMI) =' 999.99 9 9.9999' 99.999' I 9999.9 '99999