BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

read in numeric values with decimal

 

if I am sure my numeric data is 17.2

does it make a different if I just read as 19.

 

5 REPLIES 5
rvikram
Fluorite | Level 6
If you read it in as a whole number then you will lose the detail in the decimal level.

2.1 would be the numeric informat/format for 17.1 whereas 2. Would be used for 19

In my line of work, all the decimals matter.

A little more context around your question would help.
Tom
Super User Tom
Super User

@HeatherNewton wrote:

read in numeric values with decimal

 

if I am sure my numeric data is 17.2

does it make a different if I just read as 19.

 


I do not know what you mean.  SAS has two types of variables.  Fixed length character strings and floating point numbers.  

 

Normally you read in TEXT STRINGS and if you use a NUMERIC informat you get a floating point number. 

 

You can attach a format to the numeric variable to tell SAS how to PRINT the values, but it has no effect on what values are actually stored.  So if you used the format 17.2 it will print two digits to the right of decimal point and leave 14 character spaces to print the whole number part of the value (including the negative sign for values less than zero) for a total of 17 characters.

 

When reading in values from text file you normally just use the normal numeric informat, WITHOUT ANY DECIMAL WIDTH.  SAS will know where to place the decimal point based on the appearance of the period in the string.  So a strings like:

1234567
1234.56
12.3456

will be read as 1,234,567 and 1,234.56 and 12.3456.

 

Only if the strings have explicitly not include a decimal point (to save one character in the text file) would you include a decimal width on the informat you use to read the value.  The decimal width says what power of 10 to use to move the decimal point.   

 

So if you wanted to represent the number 12,345.67 with only 7 characters you could write it as: 

1234567

And then read it using an informat like 7.2 and the result would be the number 12,345.67.

 

mkeintz
PROC Star

So you have 19 digit raw numeric values, yes?

 

And you want to know if there are any disadvantages in imputing a decimal known to be between the 17th and 18th digits.  Is that correct?

 

The short answer is: neither one of these is a good idea, if absolute precision is required, The largest consecutive whole number stored as a numeric with complete accuracy is 9,007,199,254,740,992 - which is only 16 digits.  BTW, this is true for all software using double precision floating point storage - not just SAS.

 

BUT...  are these id variables, or are they actual measures of something?  If ID variables, then store them as character values, which will maintain absolute precision.  You could make a twenty-character variable with a decimal character inserted in position 18 if you want it for readability.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Patrick
Opal | Level 21

@HeatherNewton wrote:

read in numeric values with decimal

 

if I am sure my numeric data is 17.2

does it make a different if I just read as 19.

 


It's often worth to just write a little demo program to see what happens and then eventually ask questions if the result isn't as expected and you can't find an explanation.

I'm a bit guessing what you're really asking for so here just a few "things":

1. SAS can only store numerical values with full precision up to 15 digits (and partially 16 digit numbers; the biggest integer with full precision is 9007199254740992 ).

2. If your source string is longer than 15 digits then you need of course to use an informat that matches - but SAS will not store the number with full precision (=exactly as the source string). Numeric Precision

3. For a w.d informat like 17.2: NEVER use the .w component for an informat unless you understand exactly why you're doing this. 


From the docu:

Patrick_0-1707279603233.png

Does the result of below sample code make sense to you? If not then please ask targeted questions.

data demo;
  infile datalines truncover;
  input 
    @1 source_string $19.
    @1 var1 19.
    @1 var2 17.
    @1 var3 17.2
    @1 var4 best32.
    ;
  format var1-var4 best32.;
  label 
    var1="Informat: 19." 
    var2="Informat: 17." 
    var3="Informat: 17.2" 
    var4="Informat: best32."
    ;
  datalines;
123.12
123.123
123
9007199254740992
900719925474099.2
9007199254740992111
;

proc print data=demo label;
run;

 

 

Patrick_2-1707280869129.png

As you can see in above results if using informat 17.the source string gets divided by 10**2 if it's an integer - which is almost always not what you want to happen.

You can also see in row 4 and beyond what happens if the informat is shorter than the source string - or what happens if the source string exceeds the biggest number SAS can store with full precision.

 

 

 

 

s_lassen
Meteorite | Level 14

Apart from the points from @Patrick about numeric precision, there is one more thing: If your data is represented as 17.2, you should not read them as "19.", but as "17." - the format/informat specification "17.2" means that the total length of the representation is 17, not 19 (17+2), including decimals and decimal separator.

 

Other than that, if the data representation is not always with decimals, you may be better off using the informat "17.", as "17.2" will add a decimal comma if there is none. (e.g. "1234567890" gets read as 12345678.90).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 627 views
  • 0 likes
  • 6 in conversation