The SAS %INCLUDE statement allows you to bring in code from other SAS files and execute them. This allows you to create modular code which is portable and reusable.

%include "path/to/other/";

When working in SAS, it is not obvious how to create modular code when in other languages, there are many ways to import modules and code from different sources.

To import and include code from other sources in SAS, you can use the %INCLUDE statement.

%INCLUDE allows you to bring in SAS programming statements from other sources into your SAS code.

%INCLUDE executes immediately.

Below is a simple example of the syntax of using %INCLUDE in SAS.

%include "path/to/other/";

Using %INCLUDE in SAS to Include Code from Other Sources

One simple example of using %include is if you have a file which contains formats defined by PROC FORMAT.

For example, if you have a specific way you like formatting dollar amounts, then you can create formats which you can include in different scripts.

Let’s say you have the following PROC FORMATprocedure which creates a dollar format.

proc format;
   picture our_dollar_format 
       low           - -1000000000 = '00,000,000,009.9 B' (prefix='-$' mult=.000000001)
       -1000000000 < - -1000000    = '00,000,009.9 M'     (prefix='-$' mult=.000001)
       -1000000    < - -1000       = '009.9 K'            (prefix='-$' mult=.01)
       -1000       - < 0           = '009.99'             (prefix='-$')
        0          - < 1000        = '009.99'             (prefix='$')
        1000       - < 1000000     = '009.9 K'            (prefix='$' mult=.01)
        1000000    - < 1000000000  = '00,000,009.9 M'     (prefix='$' mult=.000001)
        1000000000 -   high        = '00,000,000,009.9 B' (prefix='$' mult=.000000001)

You can save this code in a file and then include it in all of the SAS scripts you need it with %INCLUDE.

Below shows you an example of how you could use %INCLUDE to include this code in another SAS program.

%include "path/to/file/with/";

data want;
    format dollar_amount our_dollar_format;
    set have;

Including Macros with %INCLUDE in SAS

Another example of when you would use %INCLUDE in SAS is if you have a collection of SAS Macro language macros which you need in multiple projects. These macros could include macros for graphing or data cleansing.

If you have files which contain different macros, then you can include them with %INCLUDE. This will bring the macros into your SAS program and allow you to use them.

Let's say for example you have a macro which helps with data categorization in data steps which used across a company.

%macro categorize;
length department $ 30.;
if dep_code == 1 then department = "South Retail";
else if dep_code == 2 then department = "North Retail";
else if dep_code == 3 then department = "South Commercial";
else if dep_code == 4 then department = "North Commercial";
else if dep_code == 5 then department = "West Commercial";

You can then bring this macro in with %INCLUDE and use it when necessary in a data step.

Below shows an example of how you might use %INCLUDE to bring in macros in your SAS code.

%include "path/to/file/with/";

data want;
    set have;

Hopefully this article has been useful for you to learn how to use %INCLUDE in your SAS code.

Categorized in:


Last Update: March 11, 2024