## Significant Figure

Occasional Contributor
Posts: 11

# Significant Figure

We can use PROC FCMP to make user defined function to generate the significant figure result for a number in Character type. It's like a format through a function.

There are 3 functions. We use the first and second function in the third one. The last data step is for unit test.

******************************************************************************
Significant Figure(digit)
******************************************************************************;
* show a number in its significant figure form which could be widely used for
scientific number presentation ;

proc fcmp outlib=my.func.format;
function rndgt(x, sf);
* Round to the specified digit according to significant figure ;
I=if x < 1 then 0 else 1;
rd=int(log10(x)) - sf + I;
return(round(x, 10**rd));
endsub;

function extsf(x) \$30;
* Extract significant digits from a number ;
attrib sfc length=\$30;
xc=compress(put(x, BEST30.)); /* Char. of x */
* Eliminate the decimal point and leading 0's;
sfc=prxchange("s/\.//", -1, xc); /* Eliminate decimal point */
sfc=prxchange("s/0*([1-9][0-9]*)/\$1/", -1, sfc); /* Eliminate leading 0's */
return(sfc);
endsub;

function sigFig(x, sf) \$30;
* Show significant figure result as a Char. ;
attrib sfc length=\$30;
* 1. Round number ;
xr=rndgt(x, sf); /* Rounding result */
xrc=compress(put(xr, BEST30.)); /* Char. of xr */
* 2. Adding trailing 0's ;
n0=sf - length(extsf(xr)); /* Number of adding 0's */
if n0 > 0 then do; /* The case we need to add 0's */
zeros=repeat("0", n0 - 1); /* zeros we add*/
out=if index(xrc, ".") then cats(xrc, zeros)
else cats(xrc, ".", zeros); /* For integer, adding decimal point */
end;
else out=xrc;
return(out);
endsub;

options cmplib=my.func;

* TEST ;
data one;
length result \$30.;
input x;
result=sigFig(x, 3);
l=length(result);
datalines;
0.012
0.01235
0.01234
0.0001
1.1
1.125
100.1
;
Clinical Programmer, Pfizer
Super User
Posts: 9,427

## Re: Significant Figure

And the question is?

Now I personally strongly disagree with the use of fcmp.  There is an industry wide disease called code hiding, which causes unimaginable suffering to programmers.   It has long been that code would either not be passed back to the owning company, or that some code would be "hidden" in compiled macro libraries.  This is bad in several ways - first that code can neither be seen nor edited by the end user, hence can never be considered in terms of proper oversight.  Also, future versions of the software have made these libraries no longer function at all - 32bit versus 64bit.  This is a big problem where companies are responsible for the product long after studies have finished.  Finally the concept of hiding code, and thankfully this is starting to be addressed, is in itself very bad.  With the need to supply code to the FDA and such groups this will hopefully eradicate this practice and bring about a harmonious open source plain text code files world.

Now why do I mention this?  Well, macros can be quite easily picked up in code, by use of special characters, so reading through the code you can pick out parts which are hidden in these compiled libraries.  Fcmp however doesn't have that limitation, and that makes it far worse from a programmer point of view.  Take the code here, if 5-10 years down the line, some other programmer gets this code alone:

data one;
length result \$30.;
input x;
result=sigFig(x, 3);
l=length(result);
datalines;
0.012
0.01235
0.01234
0.0001
1.1
1.125
100.1
;
run;

Where will the user find the SAS provided documentation for the sigFig() function?  There is nothing to indicate its not a SAS provided function, nor (assuming compiled somewhere, not provided etc.) any way of understanding what that is doing.  Sure your name is pretty good in that it confers some information, but it could just as easily randomise the number and print a tweety bird to the log.

/rant, but you can see why compiled libraries and moreso proc fcmp are really bad for the open source, open practice world we should be moving towards.

Posts: 2,833

## Re: Significant Figure

Code to compute significant digits needs to be hidden? I could understand somehwat if you had some proprietary algorithm that you want to hide ... but significant digits??

Anyway, there are much simpler (in fact, 1 line of code) ways to compute significant digits. For example, message 8 here: https://communities.sas.com/t5/Base-SAS-Programming/counting-significant-digits/td-p/90609

--
Paige Miller
Super User
Posts: 23,320

## Re: Significant Figure

@CCW if you're trying to share resources, you can post your functions in the Library, rather than as a forum. The forum is primarily for questions.

Occasional Contributor
Posts: 11

## Re: Significant Figure

Thank you! I've contacted the Admin to let me share materials in Library.
Clinical Programmer, Pfizer
Discussion stats
• 4 replies
• 110 views
• 3 likes
• 4 in conversation