Wednesday, April 5, 2017

Formats the Easy Way (SAS)

Many SAS websites and SAS articles that exist on the internet describe SAS Formats in an incredibly esoteric manner. There are many ways to create/store/import formats, however, in this article, I will attempt to show you the simplest and most effect effective method of format utilization.

For example let’s say that you have a SAS data set that contains minor league hockey players, their primary position, and their favorite beverage.

Now, let’s say that you would like to create a new data set that displays the full title of the player position instead of the abbreviation.

The example that I am providing contains only a few observations. The methodology that I will be demonstrating is more useful when formatting larger sets.

Step 1: You will need a list of the distinct variables that you wish to provide formats. This can be accomplished by running a PROC FREQ on the data set.

In the case of our example, the code would look like:

Proc Freq Data = MinorLeague;
Tables Player_Position;

The output should resemble something such as:

You now need to highlight the data output with your mouse and copy (Ctrl + C) it to a Microsoft Excel workbook (Ctrl + V).

Once this has been established, we can move on to Step 2.

Step 2: Assuming that you do not have an already pre-established list of formats existing within an Excel workbook, you will have to create the format entries manually within Excel. Simply type the format that you wish to apply in a column adjacent to the variable name that you would like the format to be assigned to.

Step 3: Utilize the concatenation function provided by Excel to create a new column containing the following the data. The final goal of such is to create a variable that can be utilized in the creation of a format. In the case of our hockey player position example, you would want to use the Excel concatenation function to format the data.

Step 4: Once you have completed the previous step, you will want to complete the formatting code within the Excel Workbook. The reason for writing the code in Excel is due to SAS’s tendency to slow down/freeze when there is too much typed within an enhanced editor window. Completing the code in Excel allows you to copy and paste the finished product directly into SAS and then immediately save it.

Step 5: Copy and paste the cells that contain the format code into the SAS editor. Next save the SAS code file.

Now instead of having to mess around with SAS Libraries and other methods of invoking SAS formats, the format can be directly invoked from its file path by utilizing %INCLUDE. Simply place the following code in the header portion of your code:

%INCLUDE "<path of code format destination>";

Highlight the code and run it to invoke the format.

Next, all that is necessary to continue is to create a new data set that applies the format.

The final version of the data set will resemble:

Utilizing this method to create formats and import them through the utilization of the %INCLUDE Statement, I feel, creates cleaner and more universalized code.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.