Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.

Slides:



Advertisements
Similar presentations
Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Advertisements

Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Copyright © 2010 SAS Institute Inc. All rights reserved. SAS ® Macros: Top Ten Questions (and Answers!) Kevin Russell –Technical Support Analyst SAS Institute.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71.
Review for midterm exam Dilshad M. Shahid Spring NYU.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Basic And Advanced SAS Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
I OWA S TATE U NIVERSITY Department of Animal Science Writing Flexible Codes with the SAS Macro Facility (Chapter in the 7 Little SAS Book) Animal Science.
Computing for Research I Spring 2014 January 22, 2014.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute.
SCSUG Avoiding Hand Cramps: Name2 macro and arrays for related variables November 9, am South Center SAS Users Group Austin, Texas.
Chapter 10:Processing Macro Variables at Execution Time 1 STAT 541 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Managing Passwords in the SAS System Allen Malone Senior Analyst/Programmer Kaiser Permanente.
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C Programming Tutorial – Part I CS Introduction to Operating Systems.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
CPS120 Introduction to Computer Science Iteration (Looping)
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.
Macro Overview Mihaela Simion. Macro Facility Overview Definition : The SAS Macro Facility is a tool within base SAS software that contains the essential.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Decision Making in Programs.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
Macro Variable Resolution Enio Presutto York University, Toronto, Canada.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CPS120 Introduction to Computer Science Iteration (Looping)
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Scion Macros How to make macros for Scion The Fast and Easy Guide.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to the C Language
Two “identical” programs
Microsoft Access Illustrated
Introduction to the C Language
Arrays, For loop While loop Do while loop
Chapter 7: Macros in SAS Macros provide for more flexible programming in SAS Macros make SAS more “object-oriented”, like R Not a strong suit of text ©
Creating Macro Variables in the DATA Step
Macro Variable’s scope
Defining and Calling a Macro
Global and Local Symbol Tables
Coding Concepts (Basics)
3 Iterative Processing.
C Programming Getting started Variables Basic C operators Conditionals
How Best to Use Macro Quoting Functions?
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Getting Started Download SAS Code from: In SAS: Tools  Options  Enhanced Editor, Check Box to show line numbers Change pathname in LIBNAME statement if needed Run initial part of code as indicated in SAS file

What do SAS Macros do? Write SAS code

Two Examples Simple use of macro variables Basic macro

Today’s Topics Macro Basics Macro Variables Macro Expressions Debugging Macros Efficient Macros

2 Components of Macro Facility 1. Macro Processor: Does the work 2. Macro Language: Communicates to processor How do we trigger the processor? Macro Variable: &name Macro: %name

Things to Remember Add MPRINT and/or SERROR to options line to see errors involving the macro facility The macro processor performs text substitution before any code is compiled

Example Creating a Simple Macro Begin with %MACRO name; End with %MEND name; (name is optional here) Invoke macro with %name

Example Creating a Macro Variable %LET name-of-variable=value-of-variable; Reference variable with: &name-of- variable

Example Adding Comments to Macros /* */ convention works within macros %* ; also comments code within macros

Example Enclosing Code in Macros Same as Example Use code between %MACRO and %MEND statements

Example Using Parameters in Macros Parameters allow easy modification of macro Naming conventions for parameters are same as for dataset variables Define parameters in %MACRO statement: %MACRO name(parameter, parameter, …) Parameters are macro variables

Example Conditionally Generate Code Use %IF/%THEN/%ELSE inside macros The condition in %IF statement cannot be based on a dataset variable

Macro Variables Text-only except under certain conditions Generated two ways: – Automatic – User-defined Scope of macro variables – Global – Local

Example Automatic Macro Variables Created when SAS session is started Available anywhere in SAS code List of automatic macro variables on pp274, 275 in text

User-Defined Macro Variables Many ways to create macro variables including: – %LET – Iterative %DO – %GLOBAL – %INPUT – %LOCAL – %MACRO – SYMPUT

Examples from text Table 3.2 from SAS Doc. To assign…Use… Constant text%LET Digits%LET Arithmetic Expressions%LET with %EVAL or %SYSEVALF function A null value%LET A macro variable reference%LET A macro invocation%LET Blanks and special characters%LET with %STR function Value from DATA stepCALL SYMPUT routine

Examples 4.1.2, 4.1.3, Macro Variables with Text At the end of text string, add macro variable where needed At beginning of text string, note end of macro variable with a period

Examples 4.1.6, 4.1.7, 4.1.8, Scope of Macro Variables Global: – Available anywhere in SAS Code – Automatic macro variables – User-defined macro variables created with %LET, %GLOBAL, SYMPUT Local: – Only available within macro where created – Created with %MACRO, %LET, Iterative %DO, %LOCAL

3 Types of Macro Expressions 1. Text – Examples: first example, PRINT, YES 2. Arithmetic Expressions – Examples: 1+2, 5*45, 6/7 3. Logical Expressions – Examples: &DAY=WEDNESDAY, C > H, 1 <= &NUM

From Table 6.1, p. 294 Where are Arithmetic and Logical Operators Used? Iterative %DO loops %DO %UNTIL and %DO %WHILE %EVAL( ) and %SYSEVALF( ) %IF %THEN %SUBSTR( )

Example Resolution of Expressions Text is resolved first Arithmetic and Logical operators are resolved next (see p. 295 for order)

Examples 4.2.2, Evaluating Arithmetic Expressions Use %EVAL for integers Use %SYSEVALF for floating point arithmetic

Example Logical Expressions Evaluated by comparing text If numbers are used, %EVAL is invoked automatically If non-integers are used, %SYSEVALF should be coded

Examples 4.3.1, 4.3.2, 4.3.3, Types of Errors 1. Macro Variable Resolution – Example: Name of macro variable is misspelled 2. Macro Open Code Processing – Example: Referring to a local macro variable in open code 3. Macro Compilation – Example: Referring to macro variable created with SYMPUT within the data step 4. Macro Execution – Example: Forgetting the %MEND statement

Tips for Debugging Macros 1. Check the basics: %MEND, semicolons, etc. (see list on p. 309) 2. Use SERROR, MPRINT, MLOGIC, and/or SGEN in OPTIONS line 3. Place %PUT statements in strategic places in code 4. Still having problems? See pp. 310, 311 in text for list of common problems

Elements Available in Open Code %*comment; %GLOBAL %LET %PUT %MACRO

Elements Available in Macros %DO Iterative %DO %DO %UNTIL, %DO %WHILE %END; %GOTO and %label %IF %THEN %ELSE %LOCAL %MEND

Example and p.302 in text Macro Functions %EVAL, %SYSEVALF %BQUOTE, %NRBQUOTE, %QUOTE, %NRQUOTE %LENGTH %SUBSTR, %QSUBSTR %UPCASE, %QUPCASE

Examples 4.5.1, Keys to Efficient Macros 1. Use macros only when necessary 2. Do not nest macros, i.e., create a macro within a macro 3. Assign function results to macro variables 4. Turn off system options when appropriate

More Information Course notes from Stat Packages: 322Notes.pdf