Solved
New Contributor
Posts: 3

What happens with informat (ie w.) too narrow for data?

My question is what happens if you use the input function and specify a numeric informat with a width too narrow for the data?  So for example, if your data includes the numeric value: 123456789, but you enter the input function with informat: '8.' , will the data that is read-in be truncated to 8 digits?

eg:

newVar = input(VarWithNineDigits,8.);

Will 'newVar' be truncated to 8 digits?

Sorry, I know it's a very 'newby' question- I actually don't use SAS, but I'm trying to quickly document and summarize someone else's syntax...  But hopefully it'll make it that much easier to answer

I appreciate the assistance,

Joshua

Accepted Solutions
Solution
‎01-12-2015 03:46 PM
Super User
Posts: 6,751

Re: What happens with informat (ie w.) too narrow for data?

Joshua,

There can be several complexities using the INPUT function.

First, note that SAS expects a character string (not a numeric) as the first parameter for the INPUT function.  So if you coded this:

V='123456789';

new_var = input(V, 8.);

You would indeed get NEW_VAR as numeric, with the value 12345678.

However, if you coded this instead, the result would be different:

V = 123456789;

new_var = input(V, 8.);

Since the first parameter is numeric, SAS has to perform a numeric-to-character conversion.  By default (double-check me on this!), SAS uses a 12-character format for this conversion.  So the INPUT function reads 3 leading blanks, plus '12345', and returns a value of 12345.

Finally, note that this is very different:

V = '123456789';

new_var = input(V, 8.2);

The 8.2 informat reads 8 characters as before.  It looks for a decimal point in those 8 characters and, finding none, applies the instruction .2 which says that the last two digits should fall after the decimal point.  So that combination returns 123456.78.  Similarly, consider this variation:

V = '1.23456789';

new_var = input(V, 8.2);

The informat 8.2 reads 8 characters, and finds a decimal point.  So it uses exactly what it finds, and NEW_VAR will be 1.234567.

Good luck.

All Replies
Super User
Posts: 13,498

Re: What happens with informat (ie w.) too narrow for data?

Generally it would read to the specified length. Or you could think of it as truncated before reading but not actually what happens.

This is actually a desired feature though more common with older data where transmission in text files (or cards) where a series of digits like

087301233322140056789 could be parsed with informats such that 08730 was a zip code, 123332214 was an account number and 0056789 had an implied decimal vale, read as 7.2, of 567.89.

If the number of significant digits is unknown you can work around that with a format/informat of BEST.

Note that when non-dataset output is generated if you try to display 123456789 with a width of 8 you will 1) get a message in the log that "At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format."

and the displayed value will have all 9 digits or may be displayed in scientific notation in some cases...

New Contributor
Posts: 3

Re: What happens with informat (ie w.) too narrow for data?

Hi ballardw, thanks for the quick reply ;  So just to make sure I understand you correctly, I gather that in the initially described example the variable "newvar" would end up with the value '12345678' rather than '123456789' , right?

New Contributor
Posts: 3

Re: What happens with informat (ie w.) too narrow for data?

Thank you all for your prompt replies ; I didn't realize that the INPUT function expects a string for the first parameter...  so it sounds like the author of this syntax must have been trying to convert string variables into numeric formats; I'll have to check the initial datasets to see if these variables were in fact initially stored as strings... ;  Anyways, much thanks for pointing me in the right direction

-Joshua

Solution
‎01-12-2015 03:46 PM
Super User
Posts: 6,751

Re: What happens with informat (ie w.) too narrow for data?

Joshua,

There can be several complexities using the INPUT function.

First, note that SAS expects a character string (not a numeric) as the first parameter for the INPUT function.  So if you coded this:

V='123456789';

new_var = input(V, 8.);

You would indeed get NEW_VAR as numeric, with the value 12345678.

However, if you coded this instead, the result would be different:

V = 123456789;

new_var = input(V, 8.);

Since the first parameter is numeric, SAS has to perform a numeric-to-character conversion.  By default (double-check me on this!), SAS uses a 12-character format for this conversion.  So the INPUT function reads 3 leading blanks, plus '12345', and returns a value of 12345.

Finally, note that this is very different:

V = '123456789';

new_var = input(V, 8.2);

The 8.2 informat reads 8 characters as before.  It looks for a decimal point in those 8 characters and, finding none, applies the instruction .2 which says that the last two digits should fall after the decimal point.  So that combination returns 123456.78.  Similarly, consider this variation:

V = '1.23456789';

new_var = input(V, 8.2);

The informat 8.2 reads 8 characters, and finds a decimal point.  So it uses exactly what it finds, and NEW_VAR will be 1.234567.

Good luck.

SAS Super FREQ
Posts: 9,365

Re: What happens with informat (ie w.) too narrow for data?

Hi:

Usually, you use the INPUT function to convert a number stored as text, into a numeric variable. This is frequently used when something like zip code or product number is stored as a character string and you need it to be a character. If your "VarWithNineDigits" is a character string, then the INPUT function would be appropriate, but the width would be incorrect. the last digit of the character string would be truncated, as shown in the attached screen shot. In my program, I created 3 different character variables of different lengths. Then I used the INPUT function with the correct width value; then I used the INPUT function to make a different variable using the WRONG width amount. As you can see, each value that was created with the wrong INFORMAT width had the last character truncated.

cynthia

🔒 This topic is solved and locked.