Fun with functions

I’ve been programming with SAS for over a decade now, and am a very active user on the community. Partly because there’s so much to know. Today alone I learned two new function that I didn’t know or had forgotten :).

  1. NLITERAL() function
    • SAS allows you to name variables freely with the validvarname=any option. However this means you need to refer the variable with quotes around the name and a n at the end to differentiate it from a character string, i.e. ‘Variable Name’n.  Formatting a macro variable in this structure can be cumbersome as you need a quote, concatenate and a trim function to ensure that spaces are removed.  Then you need to make sure the quotes don’t interfere in your code and resolve properly.  The NLITERAL function takes care of all of this by properly defining it as a literal, and it’s smart enough to only do it when required!
  2. CHAR() function
    • The char function allows you to isolate characters in a string. When you need to loop over a string this is incredibly useful.
  3. UPPER() vs %UPCASE() function
    • I’m familiar with this function, but what I didn’t know was that if you use it when querying a dictionary table in SAS, it slows things down, so instead you should use the %UPCASE() function.

Another day, another piece of SASsy knowledge.