Search code examples
sas

SAS Informat: What does an informat 3. mean?


I have the below example SAS code, which output ht2 using input with informat 3. My understanding of ht2 =INPUT(ht,3.) is that ht will be output if has 3 digits (including decimal). But instead in the below code all of ht2 are missing.

data left;
    input  name $ ht ;
    datalines;

dick  20.55 

sam  2.5
roger  2 
peter 200 
;
run;

data left2;

set left;
ht2= INPUT(ht,3.);
run;

SAS' Output of left2

| name     | ht   | ht2|
| -------- | ---- | ---| 
| dick     | 20.55| . |
| sam      | 2.5  | . |
| roger    | 2    | . |
| peter    | 200  | . |

Expected Output of left2

| name     | ht   | ht2 |
| -------- | ---- | --- | 
| dick     | 20.55| .   |
| sam      | 2.5  | 2.5 |
| roger    | 2    | 2   |
| peter    | 200  | 200 |



Solution

  • The informat 3. means to convert the first three bytes of the source string into a number.

    An INFORMAT converts text into a value. A FORMAT converts a value into text.

    In your code the variable HT is NUMERIC so to use it as the source for the INPUT() function SAS will have to convert the number into a character string. When it does this implicit conversion it uses the BEST12. format which will right align the result.

    The values of HT2 are missing because none of the value of HT when converted to a character string using the BEST12. format had anything but spaces in the first 3 positions. Most of your HT value need very few digits to be represented as a number. Even the longest value generated (from the number 20.55) will have 7 leading spaces.

    If the variable HT in dataset LEFT was read in as a character variable then the second data step to make LEFT2 would make more sense. But your first expected value is wrong. Because reading the first three bytes of the string '20.55' would result in the number 20 and not a missing value. If you wanted it to be missing you would need to test the length of the string in HT and only run the INPUT() function when the length was 3 bytes or less.

    data left;
      input name $ ht $ ;
    datalines;
    dick  20.55 
    sam   2.5
    roger 2 
    peter 200 
    ;
    
    data left2;
      set left;
      if length(ht)<4 then ht2= INPUT(ht,3.);
    run;