SAS and Literals

There are three types of literals in SAS that are frequently used. They typically follow the format of being quoted and having a letter at the end.

Date Literal (d)

When hard coding in a date into your code or creating a date, you need to specify it in the DATE9. format, and add a d at the end. For example:

date_var = ’31Jan2016’d;

You aren’t required to put in a 4 year date, but I highly recommend it. Another way to reference a date without using a literal is to use the MDY function. 1,

x = MDY(1, 31, 2016);

With the dates specified in this format you can use it in your code, either assign it to a variable or use it for comparison. The following two lines of code will result in the same thing

if x=’31Jan2016’d then do;

if x=mdy(1, 31, 2016) then do;

Datetime Literal

Along those same lines, you can also specify a datetime variable in SAS, except this time you use the letters dt.

datetime_literal= ’31Jan2016:04:20:00’dt;

Again you can also use a function, in this case, its the DHMS() function.The first parameter in this case is date, followed by hour, minute and seconds.

datetime_function = dhms(’31Jan2016’d, 4, 20, 0);

Name Literal

The last one is more rare to see, because SAS usually doesn’t allow it. EG does have it allowed by default. SAS variable names have restrictions, such as no spaces and they cannot start with numbers. If you want to ignore those restrictions you can set the option validvarname. The default value is V7.

 

option validvarname=any;

With this option you can create a variable that has a name such as ‘My Variable’

To reference it, you then use the letter n to distinguish it from a character string.

‘My Variable Name’n

 

Trust in Data Science

Trust – a person on whom or thing on which one relies (Dictionary.com)

Data Science is a difficult field.

There isn’t always certainty in the answers, methods or recommendations. What works today may not work tomorrow or in a new field. A new algorithm speeds things up, new information changes the best method, best practices change. Staying on top of all of that is most definitely a challenge. Being a CEO, CIO, or even a Chief Data Scientist is a daunting task. Which is why the people who do the actual day to day work need to be trust worthy. As the CEO or a colleague, I need to trust that you know what you’re doing and that what you recommend is something I can go along with. I may not always be in a place to verify every detail of your work, or sometimes even understand it.

So if you’re a douchebag online, insult people online, make sexist remarks in the office at the water cooler or the corporate jet you can’t really be trusted in my opinion. In general, when you can’t trust someone you don’t trust the work they do.

So don’t be a douche bag. Online or in real life. Is that really so difficult?

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.