Solved
Contributor
Posts: 61

# Measure the max length of decimal places

Hi, All,

I ran into a confusion situation when trying to measure the max decimal places in a data. The problem is demostrated in the following sample. Basically, I have a variable (pct) that has a very long decimal place (12). The first attempt to measure its decimal place returned 10. After I re-formated it into a numeric 20.12, the second attempt still gives out a 10. If you run 'proc contents', the format shows 20.12.

Of course, I don't need data with that many decimal places in my work.  I just want to know why this happened and how to measure the max decimal places accurately if needed. Thanks.

/*1. the sample data*/
data have;
pct=0.123456789123;
run;

/*2. check the length */
data checkthelength;
set have;
sca=scan(pct,2,".");
len=lengthn(scan(pct,2,"."));
run;

/*3. Reformat PCT*/
proc sql;
alter table checkthelength
modify pct format= 20.12
;quit;

/*4. check the length again*/
data checkthelength2;
set checkthelength;
sca2=scan(pct,2,".");
len2=lengthn(scan(pct,2,"."));
run;

Accepted Solutions
Solution
‎03-22-2016 12:43 PM
Super User
Posts: 9,599

## Re: Measure the max length of decimal places

This works finne for me:

```data want;
format val 20.12;
val=0.123456789123;
len=lengthn(scan(put(val,20.12),2,'.'));
run;```

I assume that when you convert to text you are truncating somehow, check your put() function.

All Replies
Solution
‎03-22-2016 12:43 PM
Super User
Posts: 9,599

## Re: Measure the max length of decimal places

This works finne for me:

```data want;
format val 20.12;
val=0.123456789123;
len=lengthn(scan(put(val,20.12),2,'.'));
run;```

I assume that when you convert to text you are truncating somehow, check your put() function.

Posts: 1,245

## Re: Measure the max length of decimal places

Hi @jiangmi,

The explanation for your results is:

SCAN is a character function, i.e. it actually expects a character value as its first argument. Just for your convenience, SAS automatically converts a numeric first argument to a character value before processing it. This is documented in the log by a message like

`NOTE: Numeric values have been converted to character values at the places given by ...`

You didn't ignore this log message, did you? :-)

For automatic numeric-to-character conversion SAS uses a default format: BEST12. This is independent of a numeric format that you might have associated with a numeric variable, i.e., your ALTER/MODIFY statement could not influence the result.

The BEST12. formatted value of 0.123456789123 is, of course, 0.1234567891, hence the 10 decimal places.

Contributor
Posts: 61

## Re: Measure the max length of decimal places

[ Edited ]

@FreelanceReinhard

Thanks for the detailed explanation. It makes sense.
I did overlook the NOTE, ^_^.

Posts: 1,245

## Re: Measure the max length of decimal places

Another important point is that the concept of the "number of decimal places" of the value of a numeric variable in SAS is more difficult than you might think. Whenever you display such a value (be it with a PUT statement, PROC PRINT, VIEWTABLE, ...) you see a formatted value. If you didn't specify the format, SAS chooses one for you (and it's sometimes hard to say which one exactly).

The formatting is necessary because numeric values are stored internally in what is called a binary floating-point representation (see Numerical Accuracy in SAS Software for details), which is ideal for the computer, but hard to read for humans. The exact decimal value corresponding to a particular binary floating-point representation can easily have more decimal places than any of the usual SAS formats (even the long ones such as BEST32.) could display.

So, the question of the number of decimal places should ideally refer to a specific format (like 20.12 in your example), which makes the answer easy, but also somewhat unsatisfactory, because the choice of the format is arbitrary to some extent.

🔒 This topic is solved and locked.