Victor
Victor

Reputation: 17107

SAS variable value does not match the formatting specified

I have a SAS dataset. When I 'View Columns', I find a column with Type=text, length=3, informat = $3., format=$3. The value stored in this variable is 10.

But based on the attributes, should it not be stored as 010?

Upvotes: 0

Views: 405

Answers (2)

Amir
Amir

Reputation: 1000

As the variable is just text, it will just store what it is assigned. For example:

data have;
   length var1 $ 3;

   informat var1 $3.;

   format var1 $3.;

   input var1;

   datalines;
10 
010
;

The fact that it has a format of $3. will not cause it to be prefixed with a leading 0, as you will see from the documentation of the $w. format, where that is not mentioned. Also, the value could later be changed to 'ab'; in both cases the value is padded with a trailing space to make up the length of 3.

Upvotes: 1

Tom
Tom

Reputation: 51621

The attributes say you have a character variable that can hold 3 bytes (normal character encodings use one byte per character). You could store '010' in that variable or '10 ' or even ' 10'. You could also store 'ABC' or 'abc'. It is just a character variable. Note that SAS always stores fixed length character fields so shorter values are padded with spaces.

It also has optionally added the FORMAT metadata saying that when displaying the value SAS should use the $3. format. Similarly is has optional metadata that says when reading text it should use the $3. informat to convert incoming text into the value to be stored.

This metadata is NOT needed because SAS already knows how to read and display character data. If you did store values with leading spaces you might want to attach the $CHAR3. format instead so that the leading spaces are preserved when writing the value.

Upvotes: 2

Related Questions