Macro variables are always stored as text and have different characteristics to dataset variables. Variables in a dataset have a minimum length of 1, a maximum length of 32,767 and a default length of 8.
Macro variables have a minimum length of 0 and a maximum length of 65,534.
Depending on how the macro variable is ceated it may, or may not, have leading or trailing blanks removed.
Starting with a %LET statement (which removes leading and trailing blanks):
%let cvar = Cat ;
the generated macro variable has a length of 3.
Resolving this macro variable in a DATA step can result in dataset variables of various lengths:
data charvars ; length cvar1 $ 6 ; cvar1 = "&cvar" ; cvar2 = "&cvar" ; format cvar3 $10. ; cvar3 = "&cvar" ; format cvar4 $char. ; cvar4 = "&cvar" ; retain cvar5 'felidae' ; cvar5 = "&cvar" ; cvar6 = symget('cvar') ; cvar7 = '&cvar' ; run ; proc contents ; run ;
Looking at the resulting metdata reveals that there are a number of possible outcomes:
Alphabetic List of Variables and Attributes # Variable Type Len Format 1 cvar1 Char 6 2 cvar2 Char 3 3 cvar3 Char 10 $10. 4 cvar4 Char 8 $CHAR. 5 cvar5 Char 7 6 cvar6 Char 200 7 cvar7 Char 5
As might be expected, an explicit LENGTH statement sets the length attribute of CVAR1 at Compile Time, giving a length of 6, the character string is then padded with blanks.
If the Macro Variable is simply resolved and stored, then the resolution also happens at Compile Time, and CVAR2 will take its length from the first time it is encountered, giving a length of 3.
The Compile Time FORMAT statement will also set the length attribute of a variable, the 'w' value determining the length: for CVAR3 a length of 10 is set.
Each format has a minimum, maximum and default 'w' value; the default for the $CHARw. format is 8, and therefore this is the length set for CVAR4.
The Compile Time RETAIN statement will also set the length of a variable when an 'initial' value is defined. In this example the length of CVAR5 is set to 7 from the stored value of 'Felidae'.
A number of functions set the result of the characer value generated to 200 (which was historically the maximum length of a character variable) if not explicitly set elsewhere. The SYMGET function which retrieves the value of a macro variable at Execution Time is one such function, and CVAR6 will therefore take a length of 200 when setting the attribute at Compile Time as the length of the retrieved string cannot be known.
Macro variables cannot be resolved within single quotes, therefore the text string &cvar sets the length of CVAR7 to 5.