Tuesday, October 2, 2012

The CHAR and FIRST Functions

Here is a quick clarification of the CHAR and FIRST functions, which apparently are still being discovered by SAS programmers.

These two functions return individual characters of a character value. They work the same as the SUBPAD function, but while the SUBPAD function can return a substring of any length, the CHAR and FIRST functions can only return a single character. As with the SUBPAD function, the return value is a space (or blank) if no character is available to return.

These functions are defined with a length of 1, so they may use less memory than the SUBPAD function. The FIRST function returns the first character of the string argument. The CHAR function returns one character from the position indicated in the second argument. If STRING is a character variable containing a character string, these statements demonstrate the use of the FIRST and CHAR functions:

   length first_character second_character $ 1;
   first_character = first(string);
   second_character = char(string, 2);

In the CHAR function as in the SUBPAD function, if the position argument is not an integer, the function truncates to form an integer value to use.