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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s