SAS Programming Tutorial
Learn SAS Programming step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is SAS? A Beginner's Overview Learn what SAS is, where it is used (clinical research, banking, insurance), what its main parts are, and how a SAS program thinks about data, before you write any code.
- SAS Interfaces: Studio, Enterprise Guide and Batch A guided tour of the ways to run SAS code: SAS Studio in the browser, Enterprise Guide, the classic windowing environment, and batch mode with .log and .lst files.
- SAS Program Structure: Steps, Statements and Comments Learn the anatomy of a SAS program: statements and semicolons, DATA and PROC steps, step boundaries, comments, naming rules and good layout habits.
- The SAS Log: Reading Notes, Warnings and Errors Learn to read the SAS log like a professional: NOTE, WARNING and ERROR messages, the usual beginner mistakes, and a checklist for reviewing every run.
Environment and Data Steps
- SAS Libraries and Data Sets: WORK, SASHELP and LIBNAME Understand SAS libraries and data sets: two-level names, the temporary WORK library, SASHELP sample data, the LIBNAME statement, PROC CONTENTS and data set options.
- DATA Step Basics: SET, Assignments, KEEP and DROP Learn to write your first real DATA steps: reading a data set with SET, creating variables, setting LENGTH and LABEL, choosing columns with KEEP and DROP.
- The Program Data Vector and DATA Step Execution Understand the compile and execution phases of the SAS DATA step, the program data vector, _N_ and _ERROR_, implicit OUTPUT and RETURN, and explicit OUTPUT and DELETE.
- Conditional Logic in SAS: IF-THEN/ELSE, DO and SELECT Make decisions in a DATA step: IF-THEN/ELSE chains, DO groups, SELECT-WHEN, comparison and logical operators, the IN operator and how missing values and string lengths behave.
- SAS Functions and Dates: Numeric, Character and Date Functions Learn the SAS functions you will use every day: numeric and character functions, how SAS stores dates as days since 1960, and MDY, YEAR, TODAY, INTCK and INTNX.
Reading and Writing Data
- Reading Instream Data with DATALINES and INPUT Learn the three styles of the SAS INPUT statement (list, column and formatted), pointer controls, DLM and DSD, MISSOVER and trailing @ for reading data typed into your program.
- SAS Informats and Formats: Reading and Displaying Values Understand the difference between informats (reading text into values) and formats (displaying values), the common ones such as date9. and dollar10.2, and how to define your own with PROC FORMAT.
- INFILE and PROC IMPORT: Reading CSV and Excel Files into SAS Read external files in SAS: FILENAME and INFILE options (DLM, DSD, FIRSTOBS, MISSOVER), PROC IMPORT for CSV and Excel, GUESSINGROWS, and the XLSX LIBNAME engine.
- Writing Data with FILE, PUT and PROC EXPORT Write data out of SAS: DATA _NULL_ with FILE and PUT for custom text and CSV files, writing to the log, PROC EXPORT for CSV and Excel, and saving permanent data sets.
Data Manipulation
- Subsetting and Selecting Data: WHERE, IF and DELETE Choose rows and columns in SAS: the WHERE statement and operators (BETWEEN, IN, CONTAINS, LIKE, IS MISSING), subsetting IF, DELETE, OBS and FIRSTOBS, and when WHERE and IF differ.
- Sorting and BY-Group Processing: PROC SORT, FIRST. and LAST. Sort data with PROC SORT (BY, DESCENDING, OUT=, NODUPKEY), then process groups in a DATA step with the BY statement and the FIRST. and LAST. automatic variables.
- RETAIN, the Sum Statement and Running Totals in SAS Carry values from one observation to the next in SAS: the RETAIN statement, the sum statement (x + y), running totals, counters, LAG and DIF, and the common LAG-in-IF trap.
- SAS Arrays and DO Loops: Iterative, WHILE and UNTIL Repeat work across many variables with SAS arrays and DO loops: iterative DO, DO WHILE and DO UNTIL, DIM, temporary arrays, character arrays and reshaping wide data to long.
- Combining Data Sets: Stacking, Interleaving and MERGE Combine SAS data sets: stack with SET, interleave with SET and BY, match-merge with MERGE and BY, use IN= flags for inner and outer joins, handle name clashes and avoid many-to-many merges.
Procedures: PROC
- PROC PRINT and PROC CONTENTS Learn how PROC PRINT lists observations and PROC CONTENTS describes a data set, with VAR, WHERE, OBS, LABEL and NOOBS options, plus a runnable Python stand-in.
- PROC SORT and Removing Duplicates Sort SAS data sets with PROC SORT, use DESCENDING, OUT=, NODUPKEY and NODUP, understand stable ordering and why BY-group processing needs sorted data, with a Python stand-in.
- PROC FREQ and Frequency Tables Count categories with PROC FREQ: one-way and two-way tables, percentages, the TABLES options, missing values, WEIGHT and the chi-square test, with a Python stand-in.
- PROC MEANS and PROC SUMMARY Summarize numeric variables with PROC MEANS and PROC SUMMARY: statistics keywords, CLASS vs BY, the OUTPUT statement, _TYPE_ and _FREQ_, NWAY and missing handling, with a Python stand-in.
- PROC FORMAT and Custom Formats Create custom SAS formats and informats with PROC FORMAT: VALUE ranges, character formats, OTHER, multi-label and CNTLIN tables, LIBRARY= and FMTSEARCH, with a runnable Python stand-in.
- PROC TRANSPOSE and Reshaping Data Reshape SAS data with PROC TRANSPOSE: BY, ID, VAR, PREFIX, NAME= and LABEL= options, convert wide to long and back, and compare with a DATA step ARRAY, with Python stand-ins.
Macros
- SAS Macro Variables Learn SAS macro variables: %LET, resolving &name, the trailing period rule, double quotes vs single quotes, automatic variables, CALL SYMPUTX, SYMGET and scope, with a Python stand-in.
- Macro Programs and Parameters Write reusable SAS macro programs with %MACRO and %MEND, positional and keyword parameters, defaults, local variables, macro functions and the MPRINT debugging options, with a Python stand-in.
- Conditional Logic and Loops in Macros Use %IF %THEN %ELSE, %DO, %DO %WHILE and %DO %UNTIL in SAS macros to generate repeated code, loop over lists, process many data sets, and know when to use DATA step logic instead.
SQL in SAS
- PROC SQL: SELECT Basics Query SAS data with PROC SQL: SELECT, WHERE, GROUP BY, HAVING, ORDER BY, the CALCULATED keyword, formats and labels, NOPRINT and QUIT, with runnable standard SQL examples.
- PROC SQL: Joins and Subqueries Join SAS tables in PROC SQL with inner, left, right and full joins, compare with the DATA step MERGE, use subqueries, IN, EXISTS and set operators, with runnable standard SQL examples.
- PROC SQL: Creating Tables and Macro Variables Create, change and drop tables in PROC SQL with CREATE TABLE AS, INSERT, UPDATE, DELETE and ALTER, and store query results in macro variables with SELECT INTO, with runnable SQL examples.
Statistics and Reporting
- PROC UNIVARIATE and PROC TTEST Describe distributions with PROC UNIVARIATE (moments, quantiles, normality tests, histograms) and compare means with PROC TTEST (one-sample, two-sample, paired), with Python stand-ins.
- PROC CORR and PROC REG Measure relationships with PROC CORR (Pearson, Spearman, matrices) and fit linear models with PROC REG: MODEL, R-square, coefficients, residuals, OUTPUT and diagnostics, with Python stand-ins.
- ODS and Output Destinations Control SAS results with the Output Delivery System: send output to HTML, PDF and Excel, pick tables with ODS SELECT and TRACE, capture results with ODS OUTPUT, and set styles, with a Python stand-in.
- PROC REPORT and PROC TABULATE Build presentation-quality tables with PROC REPORT (DEFINE, GROUP, ACROSS, COMPUTE, BREAK) and PROC TABULATE (CLASS, VAR, TABLE dimensions and statistics), with a runnable Python stand-in.
Projects and Practice
- Project: Sales Reporting Pipeline Combine PROC SORT, PROC SQL, macros, PROC MEANS and PROC REPORT into a monthly sales reporting pipeline, with a full SAS program to read and runnable SQL and Python stand-ins for each stage.
- Project: Clinical Data Validation Build a repeatable data validation program in SAS with PROC FREQ, PROC MEANS, PROC SQL checks and a macro that logs issues to one data set, with runnable SQL and Python stand-ins.
- SAS Practice Exercises Practice SAS with 12 graded exercises covering PROC steps, macro variables, PROC SQL and statistics, with solutions to read and runnable SQL and Python stand-ins to check your logic.