• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS include – Execute Code from Other Files in SAS with %include

SAS include – Execute Code from Other Files in SAS with %include

September 14, 2022 Leave a Comment

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/file.sas";

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/file.sas";

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)
;
run;

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/formats.sas";

data want;
    format dollar_amount our_dollar_format;
    set have;
run;

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";
%mend;

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/categorize.sas";

data want;
    set have;
    %categorize;
end;

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

Other Articles You'll Also Like:

  • 1.  SAS Remove Labels from Dataset with PROC DATASETS
  • 2.  nodup vs nodupkey PROC SORT Options in SAS
  • 3.  SAS if then else – Write Conditional Expressions for Multiple Conditions
  • 4.  SAS case when – Conditional Logic with case Expression in PROC SQL
  • 5.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 6.  SAS calculated – Use Columns Created from Select in PROC SQL
  • 7.  SAS left() Function – Left Align Character Variables in Data Step
  • 8.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 9.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 10.  Remove Specific Character from String in SAS

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy