Search code examples
sas

Assigning character values to numeric variables


I am new to SAS. I have been reading the SAS Certification Review Base Programming book and came across the following example and question that confuse me.

enter image description here

The top page is the example. I am confused as the first array is an array defined to contain 3 numeric variables, instead in the output each element in the first array contains character values. How is that possible?

The bottom page is a question related to the example, the correct answer is C and I do not understand why.

I know the the default byte size for a numeric variable is 8 bytes but newfirst array is an array containing 3 character variables which do not have default sizes.

Any suggestion or comment is welcomed!


Solution

  • The statement in the first slide "if an array is based on new character variables, the elements must be defined as character with a byte size" is clearly false, because the example on the second slide will run without errors. In fact you can use an ARRAY statement to create new character variables, without specifying a byte size, e.g.:

    data have ;
      array foo{3} $ ;
    run ;
    proc contents ;
    run ;
    

    That will return:

    Alphabetic List of Variables and Attributes

    #    Variable    Type    Len
    
    1    foo1        Char      8
    2    foo2        Char      8
    3    foo3        Char      8
    

    So basically you didn't specify a length for the variables created by the ARRAY statement, so SAS went with a default length, and apparently 8 is the default.

    Generally it's a good idea to specify the length of character variables when you create them, because depending on how you create them, SAS can choose different default lengths (8, 200, 32767...).