 Avoid repetitious SAS code  Create generalizable and flexible SAS code  Pass information from one part of a SAS job to another  Conditionally execute.

Slides:



Advertisements
Similar presentations
Effecting Efficiency Effortlessly Daniel Carden, Quanticate.
Advertisements

VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
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.
The Web Warrior Guide to Web Design Technologies
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71.
Computing for Research I Spring 2011 January 19, 2011
1 SAS Formats and SAS Macro Language HRP223 – 2011 November 9 th, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
An Introduction to Programming with C++ Fifth Edition
VBA Modules, Functions, Variables, and Constants
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Data Cleaning 101 Ron Cody, Ed.D Robert Wood Johnson Medical School Piscataway, NJ.
“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.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
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 PROC REPORT PROC TABULATE
Introduction to Shell Script Programming
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
Key Data Management Tasks in Stata
Adventures in ODS: Producing Customized Reports Using Output from Multiple SAS® Procedures Stuart Long Westat, Durham,
Math 3400 Computer Applications of Statistics Lecture 1 Introduction and SAS Overview.
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.
Input, Output, and Processing
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.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Chapter 5 Reading and Manipulating SAS ® Data Sets and Creating Detailed Reports Xiaogang Su Department of Statistics University of Central Florida.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Copyright © 2010, SAS Institute Inc. All rights reserved. SAS ® Using the SAS Grid.
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.
FOR MONDAY: Be prepared to hand in a one-page summary of the data you are going to use for your project and your questions to be addressed in the project.
Quick Test Professional 9.2. Testing Process Preparing to Record Recording Enhancing a Test Debugging Running the Test and Analyzing the Results Reporting.
1 PEER Session 02/04/15. 2  Multiple good data management software options exist – quantitative (e.g., SPSS), qualitative (e.g, atlas.ti), mixed (e.g.,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
SAS Programming Training Instructor:Greg Grandits TA: Textbooks:The Little SAS Book, 5th Edition Applied Statistics and the SAS Programming Language, 5.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 16 & 17 By Tasha Chapman, Oregon Health Authority.
JavaScript Syntax and Semantics
Two “identical” programs
Conditional Processing
Lesson 8 - Topics Creating SAS datasets from procedures
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 ©
Macro Variable’s scope
T. Jumana Abu Shmais – AOU - Riyadh
Defining and Calling a Macro
Presentation transcript:

 Avoid repetitious SAS code  Create generalizable and flexible SAS code  Pass information from one part of a SAS job to another  Conditionally execute data steps and PROCs  Dynamically create code at execution time

 A macro is a way to automate a task that you perform repeatedly or on a regular basis.  It is a series of commands and actions that can be stored and run whenever you need to perform the task.  Can you think of situations where you may want or have used a macro?

 In essence all of the PROC statements in SAS are macros. They have been written, validated and incorporated into the system so that they can be used repeatedly when called to perform specific tasks.

 Provides data summarization tools to compute descriptive statistics for variables across all observations and within groups of observations. › Calculates descriptive statistics based on moments › Estimates quantiles, including the median › Calculates confidence limits for the mean › Indentifies extreme values › Performs a t test

 A tool for extending and customizing SAS and for reducing the amount of text you must enter to do common tasks.  Enables you to assign a name to character strings or groups of SAS programming statements.

 The macro processor  The portion of SAS that does the work  The macro language  The syntax that you use to communicate with the macro processor

Macro statements Standard SAS statements Macro processor

 &name › Refers to a macro variable › The form &name is called a macro variable reference.  %name › Refers to a macro › The form %name is called a macro call.

 Macro Variables and Using them for specific functions › SAS defined macro variables › How do we use these in conjunction with SAS code?  Creating SAS Code Using Macros › Defining SAS macros › SYNTEX for SAS macros › Calling SAS macros  Applications  Examples

 An efficient way of replacing text strings in SAS code.  Can be defined within a macro definition or within a statement that is outside a macro definition, referred to as OPEN code.  Are independent of SAS data set variables.

 When you invoke SAS, the macro processor creates automatic macro variables that supply information related to the SAS session.

Some automatic SAS macro variables SYSCMDLAST NON-SAS COMMAND ENTERED SYSDATECURRENT DATE IN DATE6. OR DATE7. FORMAT SYSDAY CURRENT DAY OF THE WEEK SYSDEVICCURRENT GRAPHICS DEVICE SYSDSNLAST SAS DATASET BUILT SYSINDEXNUMBER OF MACROS STARTED IN JOB SYSINFO SYSTEM INFORMATION GIVEN BY SOME PROCS SYSPRODINDICATES WHETHER A SAS PRODUCT IS LICENSED SYSSCPOPERATING SYSTEM WHERE SAS IS RUNNING SYSTIMESTARTING TIME OF JOB SYSVERSAS VERSION

 To use an automatic macro variable, reference it with an ampersand followed by the macro variable name. › Example: SYSDATE and SYSDAY  SYSDATE contains a SAS date value in the DATE7. format, which displays a two-digit date, the first three letters of the month name, and a two-digit year.  SYSDAY contains a SAS day value

 Scope – Global vs Local › %GLOBAL – used to define a global macro variable  Naming conventions › Must start with a letter or _ › Can be followed by letters or digits › Cannot be a reserved word (HANDOUT 1)

 Begin with a % and a macro keyword and end with a semicolon (;)  Assign values, substitute values, and change macro variables  Can branch or generate SAS statements conditionally

%LET How do we use this? %LET CITY=CHARLESTON; TITLE “Demographics Data for &CITY”;

%LET CITY=CHARLESTON; TITLE “Demographics Data for &CITY”; Notes: 1. Once a macro variable is defined, you must precede the name of the macro variable with an ampersand (&) each time it is used. 2. If a macro variable appears within a quoted string, double quotes must be used to ensure that the macro variable ‘resolves’ to its assigned value.

 %DO  %GLOBAL  %LOCAL  %MACRO and %MEND

 Processes one or more arguments and produces a result.  Can use all macro functions in both macro definitions and open code.  Examples: (%EVAL, %LENGTH, %UPCASE, %PUT)

%LET A=1+2; %LET B=10*3; %LET EVAL_A=%EVAL(&A); %LET EVAL_B=%EVAL(&B); %PUT &A IS &EVAL_A; %PUT &B IS &EVAL_B;  SUBMIT TO GET:

%LET A=HAPPY; %LET B=BIRTHDAY; %PUT THE LENGTH OF &A IS %LENGTH(&A).; %PUT THE LENGTH OF &B IS %LENGTH(&B).;  SUBMIT TO GET:

%LET A=2; %LET B=5; %LET OPERATOR=+; %PUT THE RESULT OF &A &OPERATOR &B IS %EVAL(&A &OPERATOR &B).;  SUBMIT TO GET:

%LET DESSTAT=%STR(PROC MEANS;RUN;); … &DESTAT;  WHAT HAPPENS WHEN THIS IS RUN?

 Double quotation marks, i.e. “ “ vs ‘ ‘  Variable length  Special characters  Condition operations  Complex tasks

 Macro Processing › Need to define what the macro function will be › Process that SAS uses to program macro elements  Macro Programs › Offers more programming flexibility than macro variables in that you can pass information to parts of the macro program via macro parameters

 Can include: › Constants such as literals, variables, names, and statements › Assignments to macro variables › Macro programming statements › Macro language functions › Invocations of other functions › Nested macro definitions › LOGIC to conditionally generate SAS code

%MACRO macro-name; macro text %MEND macro-name;

%MACRO FUNMAC (DATASET, VAR1, VAR2); DATA RTEMP; SET &DATASET; RUN; PROC MEANS; VAR &VAR1 &VAR2; RUN; %MEND FUNMAC; %FUNMAC (RDATA, AGE, WEIGHT);

 Vitals data over time  Summary statistics using PROC TABULATE › Categorical variables › Continuous variables

DATA FORM05; SET ALISAH.FORM05; SUBJECTID=ZSUBJECTID; MAX_CVPRESS=F05Q02; WEIGHT=F05Q06; MAX_SBP=F05Q07; MAX_DBP=F05Q09; MAX_HR=F05Q13; KEEP SUBJECTID ZVISITID MAX_CVPRESS WEIGHT MAX_SBP MAX_ DBP MAX_HR; RUN; PROC SORT DATA=FORM05 OUT=FORM05T;BY SUBJECTID ZVISITID; RUN;

*MACRO TO CREATE MULTIPLE DATA SETS-ONE FOR EACH DAY OF VITALS; %MACRO VITALS(DAY, VISIT, DATASET); DATA &DAY._VITAL (KEEP=SUBJECTID MAX_CVPRESS& WEIGHT&DAY MAX_SBP&DAY MAX_DBP&DAY MAX_HR&DAY); SET &DATASET; IF ZVISITID=&VISIT; MAX_CVPRESS&DAY=MAX_CVPRESS; WEIGHT&DAY=WEIGHT; MAX_SBP&DAY=MAX_SBP; MAX_DBP&DAY=MAX_DBP; MAX_HR&DAY=MAX_HR;

LABEL MAX_CVPRESS&DAY="MAXIMUM CENTRAL VENOUS PRESSURE&DAY."; LABEL WEIGHT&DAY="WEIGHT&DAY."; LABEL MAX_SBP&DAY="MAX SYSTOLIC BLOOD PRESSURE&DAY."; LABEL MAX_DBP&DAY="MAX DIASTOLIC BLOOD PRESSURE&DAY."; LABEL MAX_HR&DAY="MAX HEART RATE&DAY."; RUN; PROC SORT DATA=&DAY._VITAL; BY SUBJECTID; RUN; %MEND VITALS;

*CALLING MACRO TO CREATE THE DATA SETS FOR VITALS; % VITALS (BASE, 1, FORM05); % VITALS (D1, 2, FORM05); % VITALS (D2, 3, FORM05); % VITALS (D3, 4, FORM05); % VITALS (D4, 5, FORM05); % VITALS (D5, 6, FORM05); % VITALS (D6, 7, FORM05); % VITALS (D7, 8, FORM05);

/*MACRO TO CREATE NEW VARIABLES TO BE USED IN MERGED DATA SET TO CALCULATE DIFFERENCE EACH DAY FROM BASELINE*/ %MACRO CHANGES(NEWVAR, VAR1, VAR2, VITAL, DAY); IF &VAR1 NE. AND &VAR2 NE. THEN %DO; &NEWVAR=&VAR1-&VAR2; %END; LABEL &NEWVAR="CHANGE IN &VITAL. FROM BASELINE TO &DAY."; %MEND CHANGES;

*CATEGORICAL MACRO BY TREATMENT GROUP; %MACRO CATEGORY( INDATA, MYVAR, MYTITLE) ; DATA INDATA;SET &INDATA; RUN; PROC TABULATE DATA=INDATA MISSING FORMAT=7.0 STYLE=[FONT_SIZE=1.5]; CLASS &MYVAR ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE] ; TABLE ALL="ALL SUBJECTS" &MYVAR=" ", (ZTREATMENTCODE ALL="ALL GROUPS")*(N PCTN *F=PCTFMT7.) /BOX=[LABEL="&MYTITLE“ STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]] MISSTEXT="0" PRINTMISS; LABEL ZTREATMENTCODE="TREATMENT GROUP"; KEYLABEL N="N" PCTN="PERCENT"; CLASSLEV &MYVAR ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; KEYWORD ALL N PCTN /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; RUN; %MEND;

TREATMENT GROUP ALL GROUPS TIER 1 (0.625 G/KG) TIER 2 (1.25 G/KG) TIER 3 (1.875 G/KG) NPERCENTN N N GENDER ALL SUBJECTS % % % MALE525.0%735.0%114.3%1327.7% FEMALE1575.0%1365.0%685.7%3472.3%

%MACRO CONTINUOUS (INDATA, MYVAR, MYTITLE); PROC TABULATE DATA=&INDATA FORMAT=10.0 STYLE=[FONT_SIZE=1.5]; VAR &MYVAR/STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; CLASS ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; TABLE ALL="ALL RANDOMIZED" &MYVAR*(N="TOTAL_N" MEAN*F=6.2 STD="SD"*F=4.2 MEDIAN MIN MAX), (ZTREATMENTCODE ALL="ALL GROUPS") /BOX=[LABEL="&MYTITLE" STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]]; KEYLABEL N=" "; CLASSLEV ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; KEYWORD ALL N MEAN STD MEDIAN MIN MAX /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; RUN; %MEND;

TREATMENT GROUP ALL GROUPS TIER 1 (0.625 G/KG) TIER 2 (1.25 G/KG) TIER 3 (1.875 G/KG) AGE AT BASELINE TOTAL N Mean SD Median Min Max

 SAS macros can make programming much easier.  Not all programs are enhanced by SAS macros.  If you do choose to incorporate SAS macros: › Syntax is crucial!! › Debugging can be tricky!! › Efficiency can be tremendously improved!

 Always write them one piece at a time  Write your program using standard SAS code first.  After debugging that, add %MACRO and %MEND.  Then add your parameters.