1 The SAS Environment

Working with SAS frequently involves use of:

  1. Libraries
  2. Program Editor
  3. Output Window
  4. Log Window

2 Libraries

SAS libraries are locations for your SAS data sets. Just as you can store files in multiple locations on a hard drive, you can also store data in multiple libraries within SAS.

Typically, however, you work with only two libraries: the work library and a library you specify.

  1. The WORK library
    • a temporary library
    • the default library
    • SAS data sets stored here are temporary!
  2. A specified library
    • must be created at the start of every SAS session
    • uses a libref (short for library reference)
    • SAS data sets stored here are permanent

This code below will create a library called mylib, which is the libref. The libname statement points to a SAS-Data folder on the C:\ drive. Note that pointing to physical locations on your machine is not supported in SAS OnDemand.

libname mylib 'C:\SAS-Data';

There are rules to SAS librefs, the most important of which is that they are limited to 8 characters. See this SAS doc for more information.

This youtube video shows you how to make a permanent library in SAS OnDemand.

3 The Program Editor

This is where you write your SAS program.

3.1 Exercise

Let’s export some data from a built-in R data set and then import it into SAS.

3.1.1 R export

head(morley) #shows the first few rows of the morley dataset
##     Expt Run Speed
## 001    1   1   850
## 002    1   2   740
## 003    1   3   900
## 004    1   4  1070
## 005    1   5   930
## 006    1   6   850
write.csv(morley, 'C:/Users/T530/Desktop/morley.csv') 
#writes the morley data set to my desktop

3.1.2 SAS import

A few notes on what I did before importing:

  1. uploaded morley.csv to my sasuser.v94 folder
  2. used the import wizard, which generated the code below
/* Generated Code (IMPORT) */
/* Source File: morley.csv */
/* Source Path: /home/rileymy0/sasuser.v94 */
/* Code generated on: 12/23/18, 2:18 PM */

%web_drop_table(WORK.IMPORT);

FILENAME REFFILE '/home/rileymy0/sasuser.v94/morley.csv';

PROC IMPORT DATAFILE=REFFILE
    DBMS=CSV
    OUT=WORK.IMPORT;
    GETNAMES=YES;
RUN;
    
proc contents data=work.import; run;

%web_open_table(WORK.IMPORT);

The code can be described as follows:

  1. the first four lines are comments
  2. the next line and the final line, which begin with %, are SAS macros for the OnDemand platform
  3. the FILENAME line creates a file reference, named reffile that points to the morley.csv location
  4. the PROC IMPORT step imports the data from reffile, specifying that it is a CSV and that it has header names (getnames=yes)
  5. the OUT statement in the PROC specifies the name of the imported dataset and uses the WORK libref
    • therefore, the PROC IMPORT uses morley.csv to create a SAS data set called IMPORT in the WORK library
  6. the code then uses PROC CONTENTS to display the contents of the morley dataset

3.2 SAS Statement Rules

The code above demonstrates the following features of the SAS language:

  1. EVERY SAS statement ends in a semi-colon.
  2. SAS is NOT case sensitive
  3. programs can be written on multiple lines
  4. statements can be written on same line as other statements

3.3 SAS comments

SAS has two methods of commenting:

*This is one example of a comment;

/* This is another example of a comment */
  
proc contents data=work.import; run; /*this type is better for in-line*/
/* because this comment type doesn't have a semi-colon */

4 Log Window

In the import above, the log displayed the following:

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 70         
 71         PROC CONTENTS DATA=WORK.IMPORT; RUN;
 
 NOTE: PROCEDURE CONTENTS used (Total process time):
       real time           0.04 seconds
       user cpu time       0.04 seconds
       system cpu time     0.00 seconds
       memory              2388.78k
       OS Memory           32176.00k
       Timestamp           12/24/2018 04:09:20 PM
       Step Count                        31  Switch Count  0
       Page Faults                       0
       Page Reclaims                     481
       Page Swaps                        0
       Voluntary Context Switches        0
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           16
       
 
 72         
 73         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 85

The log is useful to check on the progress of a SAS program execution:

  1. progress displays as blue text
  2. warnings display as green text
  3. errors display as red text

The SAS log is the only place to find information relevant to debugging errors in your program.

5 Output or Results Window

This displays any output from your SAS program. For example, the PROC CONTENTS output above was displayed in the results window.

Let’s print the morley data in SAS, which will also display in the Results window.

PROC PRINT DATA=WORK.IMPORT; RUN;

Click here for the PROC PRINT output.