Lesson 1, Part 1: The SAS System, Interfaces, and Basic Concepts¶

1. Statistical Analysis System® or SAS® - An Overview¶

SAS® is a suite of business solutions and technologies to help organizations solve business problems. SAS® is widely used in government agencies, healthcare and pharmaceuticals, insurance, and policy research. It ensures the correctness, reproducibility, and auditability of the results.

SAS’s core strengths include reliability, regulatory acceptance, integrated design, and statistically rigorous procedures.

In SAS®, you can read raw data, clean it, analyze it, and produce reports—all within a single SAS program. In that sense, SAS® provides:

  • One integrated system
  • One programming language
  • One execution environment
  • One log documenting the entire workflow

By contrast, R and Python, like many other open-source computing ecosystems, typically rely on multiple independently developed packages rather than a single integrated system such as SAS® to perform comparable tasks. For example, a Python workflow may use pandas for data manipulation, NumPy for numerical computing, statsmodels or scikit-learn for statistical analysis and machine learning, Matplotlib or Seaborn for data visualization, and additional packages for generating HTML, PDF, or LaTeX reports. Because these packages are developed and maintained independently, updates may introduce changes to default settings, warnings, algorithms, or computational behavior. Consequently, ensuring reproducibility often requires careful management of package versions and their dependencies through virtual environments, Conda environments, or lock files (such as requirements.txt, poetry.lock, or conda-lock).

1. SAS Programming Language Concepts¶

Components of the SAS Language

Only selected items are shown below.¶
  • SAS language elements (e.g., SAS statements, expressions, options, formats, and functions)
  • Rules for naming variables
  • Data types
  • SAS statement types
  • System options
SAS files¶
  • SAS data sets (2 types: SAS Data Files, and SAS Views)
  • SAS catalog
  • External files
SAS libraries¶
Temporary
Permanent

2. SAS Interface¶

  • SAS Windowing Environment

    • Editor window (where you write SAS code)
    • Log window (where you check the execution of the SAS program, including notes and possible errors)
    • Output window (where you see the output of your program)
    • Result window (where all the outputs from the program in one session are indexed)
    • Explore window (where all the libraries are listed, and you can browse SAS-supported files)
  • SAS Studio via SAS ODA in the cloud

  • JupyterLab

3. Overview of Base SAS¶

3.1 DATA Step¶

(e.g., INPUT and INFILE (and/or DATALINES), SET, MERGE, and UPDATE Statements)

  • Overview of DATA Step Processing
  • Processing a DATA Step: A Walk-through
  • Reading raw standard and nonstandard data into SAS
  • Reading data in Excel format into SAS
  • Creating temporary and permanent SAS data sets.
  • Exporting SAS data sets into raw data files (Different PUT styles)
  • Controlling observations and variables in a SAS data set
  • Conditionally executing SAS statements
  • Using assignment statements in the DATA step
  • Accumulating sub-totals and totals using DATA step statements
  • Using SAS functions to manipulate numeric and character data
  • Using SAS functions to convert character data to numeric and vice versa
  • Processing data using ARRAYS and DO LOOPS
Characteristics of SAS Statements¶
  • Begin with a keyword (e.g., DATA, PROC) or keywords (e.g., CALL MISSING) and end with a semicolon.
  • Can begin and end anywhere on a line or over several lines (or several statements can be on the same line).
  • Can be entered in uppercase or lowercase.
  • Can have blank or special characters that separate words.
  • Can have words that are separated by blanks or special characters but cannot have words that are entered across lines

Two types of SAS statements are used in a data step:¶

  • Declarative statements that provide information and do their work during the compilation phase (e.g., ARRAY, BY, DROP, FORMAT, INFORMAT, KEEP, LABEL, LENGTH, RENAME, RETAIN)

  • Executable statements that result in some action during the individual iteration of the data step (e.g., ABORT, CALL, CONTINUE, DELETE, DESCRIBE, DISPLAY, DO, DO UNTIL, DO WHILE, ERROR, EXECUTE, FILE, IFTHEN/ELSE, INPUT, INFILE, GO TO, LEAVE, LINK, LIST, LOSTCARD,

MERGE, MODIFY, OUTPUT, PUT, REDIRECT, REMOVE, REPLACE, RETURN, MERGE, RETURN, SELECT, SET, STOP, and UPDATE)

Definition of a SAS Name¶

There are two types of names in SAS:

  • names of elements of the SAS language
  • names supplied by SAS users

See here for details about the SAS name tokens

Giving SAS Variable Names to Fields¶
  • Variable names must start with a letter or an underscore (_), and they can contain only alphanumeric characters and the underscore.

  • Variable names can be up to 32 characters long.

  • Variable names are not case-sensitive.

  • Variable names must match the column names in the input data table that is specified when the decision is run.

  • Variable names must be unique within the decision, rule set, or code file.

Data Types¶

A SAS data set supports two types of SAS variables:

  • Numeric Variables

    • They store numeric values using floating-point or binary representation
    • They have 8 bytes of storage by default.
    • They can store 16 or 17 significant digits.
    • They can be used for performing arithmetic calculations like addition and subtraction.
    • Additionally, date-time variables are also considered numeric in SAS. Missing values for numeric variables appear as a period (.).
  • SAS Character Variable

    • They can contain any value: letters, numerals, special characters, and blanks.
    • They range up to 32,767 characters in length.
    • They have 1 byte per character.

Built-in SAS Libraries¶

SASHELP and SASUSER are built-in libraries that will always be available when SAS is invoked.

  • The SASHELP library (predefined by SAS) is where SAS has stored all the demonstration data files and catalogs; there are about 200 SAS data sets (i.e. Tables) in this library. This is a read-only library. Try the following SAS code to see the folder locations of the SASHELP library.
             %put %sysfunc(pathname(SASHELP)); 
  • SASUSER is a permanent library (predefined by SAS) that contains SAS files in the profile catalog that stores your personal settings. This is also a convenient place where users can store their own SAS files. Try the following SAS code to see the folder locations of the SASUSER library.
 %put %sysfunc(pathname(SASUSER)); 

3.2 Base SAS Procedures¶

  • Getting information about SAS data sets (PROC CONTENTS)

  • Statistical Procedures (e.g., PROC CORR, PROC MEANS, PROC UNIVARIATE, PROC SUMMARY, PROC SQL, PROC REPORT, PROC TABULATE, PROC SURVEYMEANS, PROC SURVEYFREQ)

  • Reporting Procedures for creating detailed and summary reports (e.g., PROC PRINT, PROC REPORT, PROC TABULATE)

  • Utility procedures (e.g., PROC APPEND, PROC DATASETS, PROC FORMAT, PROC SORT, PROC TRANSPOSE, PROC SURVEYSELECT)

3.3 SAS Output Delivery System (ODS)¶

  • Identifying selected objects generated by a SAS procedure
  • Storing SAS-procedure-generated selected object(s) in a SAS data set
  • Sending the output generated by a SAS procedure to a specific destination
  • Delivering output in a variety of easy-to-access procedure output files

3.4 SAS DATA Step Debugger¶

3.5 SAS Macro Facility¶

  • Symbolic substitution within SAS code
  • Automated production of SAS code
  • Conditional construction of SAS code
  • Dynamic generation of SAS code

3.6 Structured Query Language (SQL)¶

  • Read data from SAS tables
  • Add/modify/drop columns in SAS tables
  • Sort/filter rows in SAS tables
  • Create tables/views
  • Join tables/views
  • Create reports
  • Create macro variables

4. Interactive Matrix Language (IML)¶

  • high-level, matrix-vector computations
  • data analysis
  • data simulation, statistical simulation, and bootstrap computations
  • data tables (nonmatrix data structures)
  • R within PROC IML

5. SASPy¶

  • Enabling communication between Jupyter and SAS when using the SAS Kernel
  • Running Python code using commonly used IDE other than Jupyter Notebook
  • Loading SAS data sets into Python-Pandas DataFrame objects
  • Converting Python-Pandas DataFrame objects into SAS data sets
  • Using Python convenience methods on SAS data sets
  • Imitating the SAS macro facility
  • Generating SAS code from Python code
A typical SAS program is composed of one or more of the following programming steps or code components.¶
  • DATA Step

    Reading raw data into SAS
    Manipulating data

  • PROC Step

    Soring data, formatting data values, and writing reports

    Summarizing data

    Routing Log and Output to External Files

  • Macro code

    Macro variables and macros

    Macro Functions

  • Output Delivery System (ODS) code

    ODS Basics

    Controlling PROC output

    Storing any statistic created by PROCs

In the program, the user may need to use one or more Global Statements.¶
  • LIBNAME statement

  • OPTIONS statement

  • TITLE/FOOTNOTE statement

Sample SAS Programs¶

In [9]:
options nocenter number nodate nosource;
		data work.hat;
		  do x =  -5 to 5 by .5;
			do y = -5 to 5 by .5;
			  z = sin(sqrt(y*y + x*x));
			  output;
			end;
		  end;
		run;
21 The SAS System

NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1

NOTE: The data set WORK.HAT has 441 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
      

22 The SAS System

E3969440A681A2408885998500000011
In [11]:
*** Get the metadata;	
	   proc contents data = work.hat varnum;
	      ods select position;
	   run;
SAS Output

The SAS System

The CONTENTS Procedure

Variables in Creation Order
# Variable Type Len
1 x Num 8
2 y Num 8
3 z Num 8
In [13]:
*** Draw a chart in a PROC step;
		proc g3d data = work.hat;
		   plot y*x=z;
		run;
SAS Output
svgtitle 3-D surface plot of z by y and x -5.00 -1.67 1.67 5.00 x -5.00 -1.67 1.67 5.00 y z -1.00 -0.33 0.33 1.00 3-D surface plot of z by y and x

Different Faces of DATA Step¶

  • Scenario 1: Below is a SAS DATA step program with multiple OUTPUT statements, no INPUT, INFILE or DATALINES statement, and no DO loop. This code snippet simply creates a SAS data set, adding multiple observations through the OUTPUT statements. Note that the DATA step creates observations by iterating only once.

  • Although the program is perfectly valid for demonstrating the OUTPUT statement, for larger datasets, an even more efficient approach is to use DATALINES, which separates the data values from the program logic. See the SAS code for an efficient approach under Scenario 3 below.

In [1]:
dm "clear log; clear output; clear odsresults;";
data work.have1;
 name='Kirk'; quiz1=78; quiz2=84; quiz3=82;
 ave_score = round(mean(of quiz1-quiz3),.01);
 output;

 name='Neil'; quiz1=90; quiz2=85; quiz3=86;
 ave_score = round(mean(of quiz1-quiz3),.01);
 output;

 name='John'; quiz1=82; quiz2=79; quiz3=89;
 ave_score = round(mean(of quiz1-quiz3),.01);
 output;

 name='Keya'; quiz1=78; quiz2=86; quiz3=78;
 ave_score = round(mean(of quiz1-quiz3),.01);
 output;
 run;
 title "Listing from HAVE1 sas data set";
 proc print data=work.have1 noobs label;
 label quiz1 = 'Quiz 1 score'
       quiz2 = 'Quiz 2 score'
       quiz3 = 'Quiz 3 score'
       ave_score = 'Average score';
run;
SAS Output

Listing from HAVE1 sas data set

name Quiz 1 score Quiz 2 score Quiz 3 score Average score
Kirk 78 84 82 81.33
Neil 90 85 86 87.00
John 82 79 89 83.33
Keya 78 86 78 80.67
  • Scenario 2: The programmer writes the DO loop explicitly inside the DATA step, asking SAS to repeat a set of statements a specific number of times or over a range of values and to create observations. The explicit DO loop iterates through an array of quiz scores for each student. The loop adds up the quiz scores, which are then used to calculate an average.

  • The program demonstrates how to use a DO loop together with conditional IF-THEN/DO statements to create multiple observations. However, this approach is not the most efficient for entering data; it is useful for illustrating the behavior of iterative DO loops. An even more efficient approach is to use DATALINES, which separates the data values from the program logic. See the SAS code for an efficient approach under Scenario 3 below.

In [3]:
data work.have2 (drop=i);
   do i = 1 to 4;
       if i = 1 then do;
         name = 'Kirk'; quiz1 = 78; quiz2=84; quiz3= 82;
       end;

       if i = 2 then do;
         name = 'Neil'; quiz1 = 90; quiz2=85; quiz3= 86;
       end;

       if i = 3 then do;
         name = 'John'; quiz1 = 82; quiz2=79; quiz3= 89;
       end;

       if i = 4 then do;
         name = 'Keya'; quiz1 = 78; quiz2=86; quiz3= 78;
       end;
       ave_score = round(mean(of quiz1-quiz3),.01);
       output;
   end;
  run;
title "Listing from HAVE2 sas data set";
proc print data=work.have2 noobs label;
label quiz1 = 'Quiz 1 score'
      quiz2 = 'Quiz 2 score'
      quiz3 = 'Quiz 3 score'
      ave_score = 'Average score';
run;
SAS Output

Listing from HAVE2 sas data set

name Quiz 1 score Quiz 2 score Quiz 3 score Average score
Kirk 78 84 82 81.33
Neil 90 85 86 87.00
John 82 79 89 83.33
Keya 78 86 78 80.67
  • Scenario 3: The implicit loop is built into this classical SAS DATA step. SAS automatically processes each record in the input dataset one at a time. You don’t see or write the loop explicitly. Instead, SAS reads one record, executes the code, writes the output (observation), and then moves on to the next record. This continues until all records have been processed.
In [3]:
OPTIONS nocenter nodate nonumber;
DATA work.HAVE3;
 INPUT Name $ quiz1-quiz3;
   Ave_Score = ROUND(MEAN(OF quiz1-quiz3),.01);
   LABEL quiz1 = 'Quiz 1 Score' 
         quiz2 = 'Quiz 2 Score' 
         quiz3 = 'Quiz 3 Score'
         Ave_Score = 'Average Score';
 DATALINES;
 Kirk  78 84 82 
 Neil 90 85 86 
 John 82 79 89 
 Keya 78 86 78 
 ;
title "Listing from HAVE3 SAS Data File - &DateRun";
PROC PRINT data=work.HAVE3 noobs label; 
run;
SAS Output

Listing from HAVE3 SAS Data File - &DateRun

Name Quiz 1 Score Quiz 2 Score Quiz 3 Score Average Score
Kirk 78 84 82 81.33
Neil 90 85 86 87.00
John 82 79 89 83.33
Keya 78 86 78 80.67
In [ ]: