BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.

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

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.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Slide 1. Slide 2 Administrivia Nate's office hours are Wed, 2-4, in 329 Soda! TA Clint will be handing out a paper survey in class sometime this week.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
“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.
1 Chapter 3: Macro Definitions 3.1 Defining and Calling a Macro 3.2 Macro Parameters 3.3 Macro Storage (Self-Study)
Computing for Research I Spring 2014 January 22, 2014.
SAS Macros ® 101 How I learned to stop worrying and love macros Alex Chaplin BCS USA Section.
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.
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
IE 212: Computational Methods for Industrial Engineering
An Introduction to Unix Shell Scripting
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters.
CPS120 Introduction to Computer Science Iteration (Looping)
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
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.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Scion Macros How to make macros for Scion The Fast and Easy Guide.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
SAS ® Global Forum 2014 March Washington, DC.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Bill Tucker Austin Community College COSC 1315
Dynamic SQL Writing Efficient Queries on the Fly
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Organization of Programming Languages
Two “identical” programs
Error Handling Summary of the next few pages: Error Handling Cursors.
Scripts & Functions Scripts and functions are contained in .m-files
Intro to PHP & Variables
CS190/295 Programming in Python for Life Sciences: Lecture 1
For Loops October 12, 2017.
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 ©
Chapter 4 void Functions
Macro Variable’s scope
Defining and Calling a Macro
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Arrays
Passing Simple and Complex Parameters In and Out of Macros
Objectives In this chapter, you will:
CPS125.
Presentation transcript:

BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.

What's a MACRO? MACRO: a rule or pattern that specifies how a certain input sequence should be mapped to an output sequence according to a defined procedure. (Wikipedia)

Facts about Macros SAS macro code is separate from SAS code Allows you to do repetitive assignments where the only difference is a variable (s) Enables great flexibility in SAS programming and saves time

Macros from beginning to %Mend 1. The % typically identifies a macro command 2. Code enclosed within %MACRO and %MEND are viewed by SAS as a program 3. You can (almost) freely mix regular SAS code with Macro code 4. Once you define a macro program, you then need to “call” (or invoke) it 5. You call a macro by %«macro name» (you don't use the word MACRO when you call it...only when you are defining it)

MACRO VARIABLE Macro variables are either defined by a %LET statement or as parameters within a macro program A macro variable is referenced in your program by preceding the macro variable name with an ampersand (&).

LAB DATA Example Consider you want to create BOXPLOTS for several lab tests: Hemoglobin, Hematocrit, Platelets Each BOXPLOT should have its own title and axis. Of course, we will use SAS for this

Lab Data ObsTESTRESULT_N_NUMvisit 1Hematocrit45.5SC 2Hematocrit51SC 3Hematocrit45.4SC 4Hematocrit44.7SC 5Hematocrit50SC 6Hematocrit46.9SC 7Hematocrit45.9SC 8Hematocrit42.8SC 9Hematocrit42.1SC 10Hematocrit55.9SC 11Hematocrit43.6SC 12Hematocrit44.6SC 13Hematocrit39.2SC 14Hematocrit47.5SC 15Hematocrit45.5SC 16Hematocrit47.9SC 17Hematocrit44.6SC

Sample SAS MACRO for BOXPLOTS

Adding Parameters to Macros Once you identify potential parameters, list them out in parenthesis separated by a comma in the MACRO declaration When you call the MACRO, you pass the values to the MACRO again in parenthesis separated by commas

What happens? What happens if you run this program? ?? Let's check the log

Don’t forget to CALL! Nothing happened because we didn't CALL the macro Here's what the log reads after we call %labs

RESULT

SAS LOG WILL LOOK DIFFERENT Note, in the SAS MACRO log, you do not get the NOTES interspersed with the PROC statements Why? Because the entire MACRO gets compiled into a bundle of code You can “look” into the bundle of code by using the MPRINT system option

%Put On occasion, you may want to “look inside” a macro variable to see what value is stored This often occurs during debugging parameters You accomplish this by the %Put statement followed by the variable name %Put writes values to the SAS LOG There are also special %Put statements: %put _all_ %put _user_ %put _local_ %put _global_

%LET Creates a macro variable and assigns it a value %LET can be used inside or outside of a macro program Syntax %LET macro-variable = ; (CAN BE A STRING OF WORDS)

EXAMPLE 2: Using %LET, %DO and %SCAN

Conclusions MACROS save time Necessary when have a large number of variables. Imagine running a regression for every variable when you have 100+ variables! Programs are reusable and easier to understand.