Opening and Creating Programs

Opening a Program

You can open SAS programs from the Folders section of the navigation pane. To open a program, expand the appropriate folder and double-click the program that you want to open, or drag it into the work area. The program opens in a new tab in the work area.

Creating a New Program

You can create a new SAS program from the Folders section of the navigation pane. To create a new program, click New button and select SAS Program. A program window appears in a new tab in the work area.
Note: You can also click New SAS Program button on the toolbar in a program window.

Running a Program

After you have written your program, you can run it by clicking Run button. If there are no errors, the results open automatically. If there are errors, the Log tab opens by default. You can expand the Errors, Warnings, and Notes sections to view the messages. When you click on a message, SAS Studio highlights it for you in the log so that you can see exactly where the message occurs in the log.
Log with Error Expanded and Selected
Note: Because you are working in a server environment, do not include the ENDSAS statement in your SAS programs. If you run a program that contains ENDSAS, reset your SAS session by clicking More application options button and selecting Reset SAS Session.

Using the Autocomplete Feature

About the Autocomplete Feature

The autocomplete, or code completion, feature in the code editor can predict the next word that you want to enter before you actually enter it completely. The autocomplete feature can complete keywords that are associated with SAS procedures, statements, macros, functions, CALL routines, formats, informats, macro variables, SAS colors, style elements, style attributes, and statistics keywords, and various SAS statement and procedure options.
Note: The autocomplete feature is available only for editing SAS programs.
This example shows the keywords and help that appear when you enter proc a in the code editor.
Example of the Autocomplete Feature in the Code Editor
In this example, you select APPEND from the list of procedures, so that proc append appears in the code editor. When you enter a space, the code editor displays a list of options for the APPEND procedure.
Options That Are Available When You Select the APPEND Procedure

How to Use the Autocomplete Feature

To use the autocomplete feature:
  1. How you open the autocomplete list depends on the keyword that you want to add.
    • If you want to add a global statement, DATA step statement, CALL routine, procedure, macro statement, or automatic macro variable, enter the first one or more letters of the keyword that you want to use.
      A window opens with a list of suggested keywords that begin with those letters.
      Options That Appear When You Type
    • If you want to specify colors, formats, informats, macro functions, SAS functions, statistics keywords, style elements, or style attributes, position your mouse pointer in a comment and press Ctrl+spacebar. To navigate through the list of options backward, press Ctrl+Shift+spacebar.
      Note: These shortcuts work even if you have deselected the Enable autocomplete option in the Preferences window. For more information, see Customizing the Code Editor.
      Options in the Code Editor
  2. You can navigate to the keyword that you want to use in several ways:
    • Continue to type until the correct keyword is selected (because the matching improves as you type).
    • Scroll through the list by using the up and down arrow keys, the Page Up and Page Down keys, or your mouse.
  3. You can add the keyword to your program by double-clicking the selected keyword or by pressing the Enter key.

Using the Syntax Help

The code editor displays brief SAS syntax documentation as you write and edit your programs. You can display the Help in the following ways:
  • Right-click a keyword in your program and select Syntax Help.
  • Start typing a valid SAS keyword, and then click a suggested keyword in the autocomplete window.
  • Position the mouse pointer over a valid SAS keyword in your program. This works only if you have selected the Enable hint option in the Editor preferences. For more information, see Customizing the Code Editor.
The SAS Product Documentation provides more comprehensive usage information about the SAS language, but the syntax help in the code editor can get you started with a hint about the syntax or a brief description of the keyword. You can get additional help by clicking links in the syntax help window as follows:
  • Click the keyword link at the top of the window to search the support.sas.com website for the keyword.
    Top of Syntax Help Window That Shows Keyword Link
  • Click the links at the bottom of the window to search for the keyword in the SAS Product Documentation, Samples and SAS Notes, and SAS Technical Papers.
    Bottom of Syntax Help Window That Shows Other Documentation Links

Adding Table Names and Column Names

From the Libraries section of the navigation pane, you can use a drag-and-drop operation to move table names and column names into the SAS code. For example, you can move the Sashelp.Cars table into the DATA option for the PRINT procedure. When you release the mouse, the fully qualified name for the table appears in your code.
Completed PRINT Procedure

Editing the Code from a Task

You can edit the code that is generated automatically when you run a task and then run it with your modifications. When you edit the code, SAS Studio opens it in a separate program window. The code is no longer associated with the original task.
To edit a program generated by a task:
  1. On the appropriate task tab in the work area, click Code to display the code that is associated with the task.
    Note: In order to edit the code that is associated with a task, you must first display the code with the task. If the task code is not displayed, click More application options button and select Preferences. Click Tasks, and then select Show Task Code.
  2. On the toolbar, click Edit. The code is opened in a new program window.
    Task Code Window with Edit Button Selected

Creating a SAS Program Package

A SAS Program Package is a file that contains a snapshot of a SAS program along with its log and HTML results. You can create a program package from code that you have written as well as code that is automatically generated when you run a task. When you open a program package in SAS Studio, you can access the code as well as the log and results without running the program again. If you make changes to the code and rerun it, the package is not automatically updated. You must save the package again to keep the changes.
Note: The program package does not include PDF or RTF results. You cannot create a program package if you are running your program in interactive mode.
To create a program package file, open the code that you want to use and click Save As button. Specify the file location and name, and then select SAS Program Package as the Save as type option.
Note: If you want to save the program or log individually or download the results as an HTML, PDF, or RTF file, you must resubmit the program after you open the program package file.

Creating a Program Summary

You can create a summary page for code that you have written as well as code that is automatically generated when you run a task. The Program Summary page is an HTML file that opens in a separate browser tab and includes information about the program execution, the complete SAS source code, the complete SAS log, and the results. To view the Program Summary page for a program, click Program Summary button.
Note: The Program Summary is available only after you have run the program.
You can also save a Program Summary file to a folder that you specify by clicking Save As button and selecting SAS Program Summary as the Save as type option. The Program Summary is saved as an HTML file.

Using Macro Variables

Macro variables can be used to add information that is obtained when a program or task is run, such as the name and version number of the application. You can reference these items within code, titles, or footnotes by preceding them with "&".
For example, you can use macros in a footnote like the following:
Generated with &_CLIENTAPP &_CLIENTAPPVERSION
Note: In addition to the following macro variables, you can use SAS macro functions and variables in your code. For information about SAS macro functions and variables, see SAS Macro Language Reference.
Macro Variable
Description
_CLIENTAPP
name of the application.
_CLIENTAPPVERSION
application version number.
_SASPROGRAMFILE
the full path and filename of the SAS program that is currently being run. This macro variable is available only for SAS program files that are saved on the same server on which your SAS Studio code is running.

Using Your Submission History

SAS Studio maintains a log with entries for each time you run a program or task. You can use this log, or submission history, to access prior versions of your submitted code. To view your submission history, click the Code tab in your program or task window. On the toolbar, click Submission history button and select the version that you want to open. The prior version of the program opens in a new window from which you can copy and paste the code as needed.
Note: The submission history is cleared when you sign off from SAS Studio.

Automatically Formatting Your SAS Code

You can use the code editor to make your programs easier to read by automatically formatting your code. When you automatically format your code, line breaks are added, and each line is correctly indented according to its nesting level. To format the code in the code editor, click Format code button.
For example, the following code is difficult to read because it lacks indention and logical line breaks:
data topn;
length rank 8; label rank="Rank";
set topn; by &category descending &measure;
if first.&category then rank=0; rank+1;
if rank le &n then output;
run;
After you use the automatic code-formatting feature, the program looks like this:
data topn;
    length rank 8;
    label rank="Rank";
    set topn;
    by &category descending &measure;

    if first.&category then
        rank=0;
    rank+1;

    if rank le &n then
        output;
run;

Working in Interactive Mode

Some SAS procedures are interactive, which means they remain active until you submit a QUIT statement, or until you submit a new PROC or DATA step. In SAS Studio, you can use the code editor to run these procedures, as well as other SAS procedures, in interactive mode.
By using interactive mode, you can run selected lines of code from your SAS program and use the results to determine your next steps. For example, the OPTMODEL procedure in SAS/OR enables you to model and solve mathematical programming models. By running this procedure interactively, you can quickly check results for parts of the program and determine whether you need to make any modifications without running the entire program.
Note: Interactive mode is available only if you are running the first maintenance release for SAS 9.4.
To run a program in interactive mode, click Go Interactive button on the toolbar. To turn off interactive mode, click Go Interactive button again. If you change modes while a program is open, the log and results for that program are cleared. You can also clear the log and results manually by clicking Clear Log or Clear Results button on the appropriate toolbar.
Note: If you create a new library when you are in interactive mode, the library might not be available when you turn off interactive mode. If you want the library to be available in both interactive and noninteractive modes, you can assign the library again in noninteractive mode, or you can add the library assignment to your autoexec.sas file. For more information, see Editing the Autoexec File.
When you run a program in interactive mode, SAS Studio does not add any automatically generated code, such as ODS and %LET statements, to your program. In addition, results are generated only in HTML. In interactive mode, the log and results are appended to the existing log and results. Previously submitted code remains active until you terminate it.
For example, suppose you have the following program:
proc sql;
  select * from sashelp.cars;
  
  select * from sashelp.class;
  quit;
In noninteractive mode, if you select the first two lines of code and submit them, the code runs successfully. If you then select the last two lines of code and submit them, the code fails because the PROC SQL statement is missing.
If you switch to interactive mode and follow the same steps, the last two lines of code run successfully because the PROC SQL statement is still active.
Note: For documentation about specific procedures, see the SAS Programmer’s Bookshelf on support.sas.com.