Writing SAS Source Files Included with a %INCLUDE Statement to the SAS Log

In SAS programming, the %INCLUDE statement is a powerful tool that allows you to include external SAS source files into your current SAS program. This can be particularly useful for modularizing code, reusing scripts, and maintaining a clean and organized workflow. In this blog post, we’ll explore how to write SAS source files included with a %INCLUDE statement to the SAS log, ensuring transparency and ease of debugging.

What is the %INCLUDE Statement?

The %INCLUDE statement in SAS is used to include the contents of an external file into the current SAS program. This can be a SAS program file, a macro, or any other text file that contains SAS code. The syntax is straightforward: %INCLUDE 'path-to-file';

Why Write Included Source Files to the SAS Log?

Writing the included source files to the SAS log can be beneficial for several reasons:

  • Transparency: It allows you to see the exact code that is being executed, which is especially useful when debugging.
  • Documentation: It provides a complete record of the code execution in the log, which can be helpful for auditing and review purposes.
  • Error Tracking: It makes it easier to identify and trace errors back to their source.

How to Write Included Source Files to the SAS Log

To write the contents of the included source files to the SAS log, you can use the SOURCE2 option in the %INCLUDE statement. The SOURCE2 option directs SAS to write the included code to the log.

Here’s how you can do it: %INCLUDE 'path-to-file' / SOURCE2;

Practical Example

Let’s walk through a practical example to illustrate how this works.

1.Create an External SAS Source File

First, create a simple SAS source file named example.sas with the following content:

/* example.sas */

data example;

    input id name $ age;

    datalines;

    1 John 25

    2 Jane 30

    3 Alice 28

    ;

run;


proc print data=example;

run; 


2.Include the Source File in Your Main Program

Next, create your main SAS program and use the %INCLUDE statement with the SOURCE2 option to include the example.sas file:


/* main_program.sas */

%INCLUDE 'C:\path\to\example.sas' / SOURCE2;


3.Run the Main Program

When you run main_program.sas, the contents of example.sas will be written to the SAS log, along with the results of the code execution.

Viewing the SAS Log

After running the main program, check the SAS log. You should see the included code from example.sas printed in the log, followed by the output of the data step and the PROC PRINT procedure.


1    %INCLUDE 'C:\path\to\example.sas' / SOURCE2;

NOTE: %INCLUDE (level 1) file C:\path\to\example.sas is file 

      C:\path\to\example.sas.

2    /* example.sas */

3    data example;

4        input id name $ age;

5        datalines;

6        1 John 25

7        2 Jane 30

8        3 Alice 28

9        ;

10   run;


NOTE: The data set WORK.EXAMPLE has 3 observations and 3 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds


11   

12   proc print data=example;

13   run;


NOTE: There were 3 observations read from the data set WORK.EXAMPLE.

NOTE: PROCEDURE PRINT used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

Conclusion

Using the %INCLUDE statement with the SOURCE2 option in SAS is a simple yet effective way to ensure that the contents of your included source files are written to the SAS log. This practice enhances transparency, aids in debugging, and provides a comprehensive record of your code execution. By incorporating this technique into your SAS programming workflow, you can maintain better control and oversight of your code.


Popular posts from this blog

Calculating Study Day in R for CDISC Compliance: A Step-by-Step Guide

Mastering the Art of Debugging Nested Macros in SAS

HOW TO ACCESS SPECIAL CHARACTERS IN SAS