Yesterday we went over how SAS can display values in different ways, so what you see in the results in now necessarily how the data is stored. Today, we’ll introduce how this applies with dates.
Interesting fact: SAS only has 2 data types, numeric and character. Dates are stored as numeric variables and we use formats to specify how we want them displayed. Let’s go over an example:
DATA myTable; /*Use input statement to declare your variables and specify their informats*/ INPUT Name $ Birthday mmddyy8.; DATALINES; John 04/19/90 Bella 05/08/76 ; RUN;
We used the informat “mmddyy8.” to tell SAS how how it to read in the date values. We used a width of 8, and not the default of 6, because the forward slashes count as characters. Think of the width as the number of characters SAS should read in. If we had used a 4 digit year instead of 2, such as 1990 instead of 90, then we would have had to use the informat “mmddyyyy” with a width of 10. You can review other informats here: SAS Informats
If you are running SAS, run the code and check out what the output looks like. You’ll see that SAS displays the values as they are actually stored: 11066 and 5972. Definitely not a very useful way to read dates. Let’s run the PROC PRINT procedure with the FORMAT statement to print our table and display the dates in a format we can actually read:
PROC PRINT; FORMAT Birthday DATE9.; RUN;
PROC PRINT is a very common procedure used for printing results, we’ll be using that a lot in the future. The FORMAT statement simply tells SAS how to display the “Birthday” variable. I chose to use the format “DATE9.” which displays dates like “19APR1990”. Check out this webpage for a list of other date formats: SAS FORMATS
SAS loves to test people about informats and formats, so it’s a good idea to memorize the most common ones and make sure you understand how they work.
Tomorrow, we’re going to take a little break from learning how to load data and how formats work to mix things up a little bit. Don’t worry, we’ll come back to it later so you’re armed with everything you need to know for the exam (if you choose to take it). I personally find this stuff a bit dry, I’m guessing you’re as eager as I am to play with data and do some analysis. Unlike other training programs, we’ll be jumping around a bit between different SAS subjects. This will hopefully keep things more interesting and will force you to review what you’ve already learned when we return to it in the future.