BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jiangmi
Calcite | Level 5

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

FreelanceReinh
Jade | Level 19

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.

jiangmi
Calcite | Level 5

@FreelanceReinh

 

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

FreelanceReinh
Jade | Level 19

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 10970 views
  • 1 like
  • 3 in conversation