Advanced SAS Programming Tutorial for Beginners - Naidu's Lecture

Jul 20, 2024

Advanced SAS Programming Tutorial for Beginners - Naidu's Lecture

Introduction

  • Speaker: Naidu from Great Online Training
  • Topic: Advanced SAS Programming Tutorial for Beginners
  • Focus: SAS Macros, SAS SQL, Graphs
  • Participants Introduction:
    • Sandhya from UK
    • Mary from the USA, Richmond, Virginia
    • Sangeeta from the USA, South Carolina
    • Aditi Rati from India

Lecture Agenda

  • SAS Macros
  • SAS SQL
  • SAS Figures/Graphs

SAS Programming Overview

  • Divided into two parts:
    • Basis Programming
    • Advanced Programming
  • Base Topics: proc report, proc frequency, proc means, proc print, proc contents,
  • Advanced Topics: SQL, graphs, macros

SAS Macros

Introduction to SAS Macros

  • Use: Automating repetitive tasks, making work faster by not needing to rewrite the same code
  • Example: Sorting multiple datasets using proc sort with and without macros
  • Concept: Automate repetitive tasks to save time and effort
  • Syntax:
    • Macro code is separated from SAS code
    • Rules: start with a character/underscore, no embedded blanks, upper or lowercase accepted
    • Triggers: % for calling a macro, & for calling macro variables

Types of SAS Macros

  • Macro Variables:
    • Amphisand sign (&): Call macro variables
    • Percentage sign (%): Call macro itself
  • SAS Macro Compiler: Macro compiler and data set compiler run the macros and provide output

Creating Macros

  • Syntax:
    %macro macro_name;
    sas_code
    %mend macro_name;
    %macro_name;
    
  • Example:
    %macro print;
    proc print data=sashelp.class;
    run;
    %mend print;
    %print;
    
    • Prints the sashelp.class dataset

Managing Macro Variables

  • Defining Macro Variables:
    %let var = value;
    %put &var;
    
    • SAS stores them globally/local
  • Important: Avoid embedded blanks or special characters except underscore
  • Length: Values up to 64KB; names up to 32 characters

Declaring Macro Variables

  • Global Variables: Accessible anywhere in the SAS session
  • Local Variables: Restricted to the current macro scope
  • Defining:
    %local var;  %global var;
    %macro test(var);
    data new;
    set old;
    run;
    %mend test;
    
  • Example (with loops): Automate creation of macro variables within loops

Macro Facility

  • License Requirement: Must have a license for macros to run them
  • Tools and Compilers: Set of commands, syntaxes, and compilers enabling macros

Usage in Macros

  • Automate code repetition
  • Creating more complex macros with parameters:
    • Positional Parameters: Defined using specific positions in the macro call
    • Keyword Parameters: Used to define specific names and values within the macro

Macros and Data Manipulation

  • Include: %include to read and execute SAS statements from an external file
  • Example:
    %include 'path_to_file.sas';
    

Advanced Macro Functions

  • Evaluation: %eval, %sysevalf
  • String Manipulations: %scan, %substr, %index
  • Conditional Processing: %if-%then/%else
  • Iteration: %do-%while, %do-%until
  • Debug Options: mprint, mlogic, symbolgen

SAS SQL Concepts

Basic SQL Running in SAS

  • Syntax:
    proc sql;
    create table my_table as
    select * from existing_table;
    quit;
    
  • Capabilities: Select, insert, update, delete
  • Example: Sorting, subsetting data
  • Joining Tables: Inner joins, outer joins, natural joins
  • Using Views: To save memory, efficient data handling
  • SQL Functions: Aggregate functions (sum, avg), string functions (concat), etc.

SAS Graphs (GCHART and PROC SGPLOT)

Types of Graphs

  • Block Chart: 3D representation of data blocks
  • Pie Chart: Circular representation, slices of data
  • Bar Chart: Vertical or horizontal bars representing data
  • Control Options: Device, gunit, border/no border, rotate, format, etc.
  • Saving Graphs: Save as .emf, .png, etc.
  • Legend and Axes Customization: Labeling, font size, colors, etc.

Final Notes

  • Macro Storage: Use libname for permanent storage
  • Debugging and Options: Use relevant options to debug and optimize macros
  • **Creating Complex Reports: Use PROC TEMPLATE for highly customized outputs

Closing Remarks

  • Practice macros in different scenarios
  • Understand SQL's power in data manipulation
  • Master various graph options for professional reporting