0
votes

When reading delimited files on SAS, one can set the length of character variables by using length statement. However, one cannot do that with plain numbers, (no commas, no dollar signs, standard data etc.). The only way to set the length of such statements inherently from what I could think of is by using a commaw. informat. Is this plausible, is there a length statement if we wanted to read just plain numbers?

I was also wondering, if I have a piece of data "$105.93", I can read it as dollar3.2. But reading it as dollar12.2 doesn't seem to make a difference. Why?

Thank you.

2
This will likely answer your question. blogs.sas.com/content/sasdummy/2007/11/20/…Reeza

2 Answers

1
votes

Variable length and format width are independent things

The LENGTH is how many bytes SAS uses to store the data in the dataset. Since SAS uses 64 bit floating point numbers the length of a numeric variable must be between 3 and 8 bytes. (You can actually store numbers in as few as 2 bytes on IBM mainframes since they used a different floating point representation.) The WIDTH used by a format or informat is how many characters it takes to print the value as text. So if you are reading or writing a string like $1,205.93 you will need to use 9 characters. But since it is a number you will want to store it with 8 bytes. So the LENGTH is 8 even if the format width is longer than that.

List mode input ignores informat widths

The second confusion is that when you are using list mode INPUT (rather than formatted mode) then SAS will ignore the width specified on any informat that you might have attached to a particular variable. Instead it will apply the informat to all of the next word that it sees before the next delimiter. You could consider that it adjusts the informat width to match the width of the data. So if ran a program like this:

data want ;
  input id cost ;
  informat cost comma1. ;
cards;
1 $103.93
;

COST will get set properly because the width of 1 specified in the informat statement is ignored. Note that the same thing applies when using the : modifier to allow the informat specification to be inserted into the INPUT statement without causing SAS to switch from list mode to formatted mode.

Misuse of the decimal part of an informat specification

On a FORMAT the decimal specification tells SAS how many digits to the right of the decimal place to print. But when you add a decimal specification on an INFORMAT you are telling SAS where to place the decimal point when the input string does not have one. Basically you are telling SAS what power of 10 to use to divide any integer values that it sees. So unless you are positive that your input stream intentionally eliminated the periods between the ones and tenths places you should never specify any decimal value for a INFORMAT. So you wouldn't want to use the DOLLAR12.2 informat. (Note also that there is no difference between the DOLLAR. and COMMA. informats. Both remove commas and dollar signs before trying to translate the string into a number.)

0
votes

When reading delimited files on SAS, one can set the length of character variables by using length statement. However, one cannot do that with plain numbers, (no commas, no dollar signs, standard data etc.).

You can specify the length of numeric variable, but length corresponds to the number of bytes a variable can store. For numeric, a byte stores more than a single digit.

http://support.sas.com/documentation/cdl/en/hostwin/69955/HTML/default/viewer.htm#n04ccixfia6l2pn1f8szvttqg3hm.htm

Here's an example that shows how it's saved.

data have;
    infile cards dlm=';';
    length V1 V2 8 V3 4;
    informat V1 dollar3.2 V2 best12. V3 dollar8.2;
    format v1 dollar12.2 v2 8. v3 dollar8.2;
    input v1 v2 v3;
    datalines4;
$12.22; 24; $1342
$145.22; 25; $13.42
$45.01; 22; $100.1
;;;;
run;

proc contents data=have;
run;