Seminar Series: CTSI Presents

Similar documents
DSCI 325: Handout 15 Introduction to SAS Macro Programming Spring 2017

Going Under the Hood: How Does the Macro Processor Really Work?

Sandra Hendren Health Data Institute

Macros are a block of code that can be executed/called on demand

A SAS Macro for Producing Benchmarks for Interpreting School Effect Sizes

SAS Macro. SAS Training Courses. Amadeus Software Ltd

Using SAS/SCL to Create Flexible Programs... A Super-Sized Macro Ellen Michaliszyn, College of American Pathologists, Northfield, IL

1 Files to download. 3 Macro to list the highest and lowest N data values. 2 Reading in the example data file

Unlock SAS Code Automation with the Power of Macros

More About SAS Macros

Epidemiology Principles of Biostatistics Chapter 3. Introduction to SAS. John Koval

Functions vs. Macros: A Comparison and Summary

A Table Driven ODS Macro Diane E. Brown, exponential Systems, Indianapolis, IN

The DATA Statement: Efficiency Techniques

SURVIVING THE SAS MACRO JUNGLE BY USING YOUR OWN PROGRAMMING TOOLKIT

Tired of CALL EXECUTE? Try DOSUBL

Program Validation: Logging the Log

Base and Advance SAS

Creating Macro Calls using Proc Freq

Using SAS to Analyze CYP-C Data: Introduction to Procedures. Overview

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

AN INTRODUCTION TO MACRO VARIABLES AND MACRO PROGRAMS Mike Zdeb, School of Public Health

SAS Display Manager Windows. For Windows

Foundations and Fundamentals. SAS System Options: The True Heroes of Macro Debugging Kevin Russell and Russ Tyndall, SAS Institute Inc.

A SAS macro (ordalpha) to compute ordinal Coefficient Alpha Karen L. Spritzer and Ron D. Hays (December 14, 2015)

Demystifying Inherited Programs

%Addval: A SAS Macro Which Completes the Cartesian Product of Dataset Observations for All Values of a Selected Set of Variables

A Macro that can Search and Replace String in your SAS Programs

SAS Online Training: Course contents: Agenda:

Cleaning Duplicate Observations on a Chessboard of Missing Values Mayrita Vitvitska, ClinOps, LLC, San Francisco, CA

An Introduction to Macros Deb Cassidy

Purchase this book at

Validation Summary using SYSINFO

SD10 A SAS MACRO FOR PERFORMING BACKWARD SELECTION IN PROC SURVEYREG

Storing and Reusing Macros

Types of Data Mining

Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA

Developing Data-Driven SAS Programs Using Proc Contents

EXAMPLE 3: MATCHING DATA FROM RESPONDENTS AT 2 OR MORE WAVES (LONG FORMAT)

Logging the Log Magic: Pulling the Rabbit out of the Hat

SAS 101. Based on Learning SAS by Example: A Programmer s Guide Chapter 21, 22, & 23. By Tasha Chapman, Oregon Health Authority

Why & How To Use SAS Macro Language: Easy Ways To Get More Value & Power from Your SAS Software Tools

AURA ACADEMY SAS TRAINING. Opposite Hanuman Temple, Srinivasa Nagar East, Ameerpet,Hyderabad Page 1

New Macro Features Added in SAS 9.3 and SAS 9.4

Posters. Workarounds for SASWare Ballot Items Jack Hamilton, First Health, West Sacramento, California USA. Paper

Using Templates Created by the SAS/STAT Procedures

Efficiently Join a SAS Data Set with External Database Tables

EXAMPLE 3: MATCHING DATA FROM RESPONDENTS AT 2 OR MORE WAVES (LONG FORMAT)

MARK CARPENTER, Ph.D.

Debugging. Where to start? John Ladds, SAS Technology Center, Statistics Canada.

USING DATA TO SET MACRO PARAMETERS

Surviving the SAS Macro Jungle by Using Your Own Programming Toolkit Kevin Russell, SAS Institute Inc., Cary, North Carolina

A Lazy Programmer s Macro for Descriptive Statistics Tables

Macro to compute best transform variable for the model

Open Problem for SUAVe User Group Meeting, November 26, 2013 (UVic)

GET A GRIP ON MACROS IN JUST 50 MINUTES! Arthur Li, City of Hope Comprehensive Cancer Center, Duarte, CA

Paper CC06. a seed number for the random number generator, a prime number is recommended

Macro Architecture in Pictures Mark Tabladillo PhD, marktab Consulting, Atlanta, GA Associate Faculty, University of Phoenix

Introduction to SAS Procedures SAS Basics III. Susan J. Slaughter, Avocet Solutions

Macros to Manage your Macros? Garrett Weaver, University of Southern California, Los Angeles, CA

Using a Control Dataset to Manage Production Compiled Macro Library Curtis E. Reid, Bureau of Labor Statistics, Washington, DC

Paper CC16. William E Benjamin Jr, Owl Computer Consultancy LLC, Phoenix, AZ

SAS Macro Programming for Beginners

SAS Programs SAS Lecture 4 Procedures. Aidan McDermott, April 18, Outline. Internal SAS formats. SAS Formats

Macro Magic III. SNUG Presentation (30mins)

A SAS Macro for Balancing a Weighted Sample

STATION

SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

DSCI 325: Handout 10 Summarizing Numerical and Categorical Data in SAS Spring 2017

TIPS AND TRICKS: IMPROVE EFFICIENCY TO YOUR SAS PROGRAMMING

Out of Control! A SAS Macro to Recalculate QC Statistics

An Easy Route to a Missing Data Report with ODS+PROC FREQ+A Data Step Mike Zdeb, FSL, University at Albany School of Public Health, Rensselaer, NY

options nofmterr; ods html close; *STOPS WRITING TO THE CURRENT RESULTS VIEWER;

Utilizing the Stored Compiled Macro Facility in a Multi-user Clinical Trial Setting

Top-Down Programming with SAS Macros Edward Heaton, Westat, Rockville, MD

Get Started Writing SAS Macros Luisa Hartman, Jane Liao, Merck Sharp & Dohme Corp.

The %let is a Macro command, which sets a macro variable to the value specified.

What Is SAS? CHAPTER 1 Essential Concepts of Base SAS Software

SAS CURRICULUM. BASE SAS Introduction

Stat 302 Statistical Software and Its Applications SAS: Data I/O

%MAKE_IT_COUNT: An Example Macro for Dynamic Table Programming Britney Gilbert, Juniper Tree Consulting, Porter, Oklahoma

Christopher Louden University of Texas Health Science Center at San Antonio

Working the System: Our Best SAS Options Patrick Thornton, SRI International, Menlo Park, CA Iuliana Barbalau, Adecco, Pleasanton, CA

%MISSING: A SAS Macro to Report Missing Value Percentages for a Multi-Year Multi-File Information System

Advanced Macro Topics Steven First, Systems Seminar Consultants, Madison, WI

ABSTRACT INTRODUCTION MACRO. Paper RF

A Practical and Efficient Approach in Generating AE (Adverse Events) Tables within a Clinical Study Environment

ABSTRACT INTRODUCTION THE GENERAL FORM AND SIMPLE CODE

Paper S Data Presentation 101: An Analyst s Perspective

22S:166. Checking Values of Numeric Variables

&&&, ;;, and Other Hieroglyphics Advanced Macro Topics Chris Yindra, C. Y. Training Associates

Using MACRO and SAS/GRAPH to Efficiently Assess Distributions. Paul Walker, Capital One

Generating Customized Analytical Reports from SAS Procedure Output Brinda Bhaskar and Kennan Murray, RTI International

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

ECLT 5810 SAS Programming - Introduction

Introduction to SAS. Cristina Murray-Krezan Research Assistant Professor of Internal Medicine Biostatistician, CTSC

SAS Macro Dynamics: from Simple Basics to Powerful Invocations Rick Andrews, Office of Research, Development, and Information, Baltimore, MD

SAS Certification Handout #11: Adv. Prog. Ch. 9-10

PROC MEANS for Disaggregating Statistics in SAS : One Input Data Set and One Output Data Set with Everything You Need

Contents. About This Book...1

Transcription:

Biostatistics, Epidemiology & Research Design (BERD) Howard Cabral, PhD, MPH Christine Chaisson, MPH Seminar Series: CTSI Presents November 20, 2014

Demystifying SAS Macros BUSPH Data Coordinating Center Leah Forman, MPH Clara Chen, MHS Michael Winter, MPH November 20, 2014

Macro Basics: an introduction Leah Forman Statistical Data Analyst Data Coordinating Center 3

SAS Macro language: References previously written SAS code and commands it to run Why use it? To repeat code, substitute text, pass values.. 4

Macro Structure defining the macro %macro Boston University examplemacro(one=, Slideshow Title Goes Here two=); /*Defines the Macro*/ /*This macro is called "examplemacro"*/ data &one; /*Code that defines what happens when the macro is used*/ set &two; if two = 3 then four = 5; %mend examplemacro; /*Ends the macro*/ 5

Macro structure executing the macro data &one; set &two; if two = 3 then four = 5; %examplemacro(one=dset, two=var2); /*This calls the macro and tells it to execute*/ When the macro runs, it s as if you are running this code: data dset; set var2; if two = 3 then four = 5; 6

Keyword parameters Enter values for keyword parameters in any order %macro keywords (dsname=, varlist=); proc freq data=&dsname; tables &varlist; %mend keywords; %keywords(dsname=one, varlist=studyid gender age) %keywords(varlist=studyid gender age, dsname=one) 7

Keyword Parameters cont d Can assign default values: %macro keywords (dsname=one, varlist=); proc freq data=&dsname; tables &varlist; %mend keywords; %keywords(varlist=studyid gender age) %keywords(dsname=two,varlist=studyid gender age) 8

Passing values with callsymputx Assign a calculated variable to a macro variable Creates a macro variable in the datastep proc means data=sug.shoe_vendors noprint; var Mfg_Suggested_Retail_Price; output out = outdat mean=meanprice; The output dataset, outdat : 9

data _null_; set outdat; call symputx ('MeanPriceMac', MeanPrice); data lowmean; set sug.shoe_vendors; where Mfg_Suggested_Retail_Price<&MeanPriceMac; 10

Debugging: %put Boston Lets University you see Slideshow the Title value Goes of Here your macro variable. Recall this example: proc means data=sug.shoe_vendors noprint; var Mfg_Suggested_Retail_Price; output out = outdat mean=meanprice; data _null_; set outdat; call symputx ('MeanPriceMac', MeanPrice); %put mean of Price is &MeanPriceMac %put mean of Price is &MeanPriceMac; mean of Price is 155.31855956 what appears in the log 11

Debugging: mprint, symbolgen 13 %macro meansy (varname=); 14 proc means data=microbiology; 15 var &varname; 16 17 %mend meansy; 18 19 %meansy(varname=tbspecnum) NOTE: Writing HTML Body file: sashtml.htm NOTE: There were 2745 observations read from the data set WORK.MICROBIOLOGY. NOTE: PROCEDURE MEANS used (Total process time): real time 3.48 seconds 12

Debugging: mprint, symbolgen 28 options mprint symbolgen; 29 %macro meansy (varname=); 30 proc means data=microbiology; 31 var &varname; 32 33 %mend meansy; 34%meansy(varname=TBSpecNum) MPRINT(MEANSY): proc means data=microbiology; SYMBOLGEN: Macro variable VARNAME resolves to TBSpecNum MPRINT(MEANSY): var TBSpecNum; MPRINT(MEANSY): NOTE: There were 2745 observations read from the data set WORK.MICROBIOLOGY. NOTE: PROCEDURE MEANS used (Total process time): real time 0.01 seconds cpu time 0.01 seconds 13

Global vs. Local Macro Variables Global: Exists for the rest of the SAS session %LET statement (outside of a macro definition) %GLOBAL statement CALL SYMPUTX Local: Exists only during execution of the macro in which it is created %LET statement (inside a macro definition) %LOCAL statement (inside a macro definition) Macro parameters 14

Global vs. local: exercise Boston Which University Macros Slideshow are Title global, Goes Hereand which are local? %let presenter=leah; %macro groupexercise (type= ); %let day=thanksgiving; title Turkey eaten on &day"; title2 "presentation by &presenter"; proc means data=turkeyday; var &type; %global type2; %let type2=&type; %mend groupexercise; %groupexercise(type=reeses hersheys) 15

Which are local, and which are global? To find out, you can use %put and they will print out in the log: %put presenter = &presenter type = &type day=&day type2=&type2 ; 16

Building a Macro: A Real World Example Clara Chen Assistant Director of Operations, Data Coordinating Center 17

Constructing a macro: output N(%) Step 1: Use example and examine ODS tables to identify locations of needed information proc sort data=bweight;by Married; ods trace on; /*Writes ODS Table Info To Log*/ proc freq data=bweight ; where ; by Married ; tables black /missing; ods trace off; 18

SAS log 19

Save output using ODS OUTPUT Step 2: Write ods tables to a temporary set for further manipulation via ods output statement proc freq data=bweight ; where ; by Married ; tables black /missing; ods output onewayfreqs=blackf; 20

blackf dataset looks like this: 21

Create summary variables Step 3: Create summary variables to use with PROC REPORT data blackmarried ; length Stat $100 Variable $32 Label $100 Level $50; set blackf; Stat = compress(frequency) " (" compress(round(percent,.01 )) ")" ; Variable = "Black" ; Level = F_black ; Label = vlabel(black); keep Married Level Label variable Stat; 22

blackmarried dataset looks like: 23

Generalize data steps for a macro Boston Step University 4: Put data Slideshow steps Title Goes together Here in a macro generalizing set, where, by variable and variable %Macro NPct(StatData=, WhereStmnt=, ByVar=, Var= ) proc sort; data=&statdata.; by &ByVar.; proc freq data=&statdata. ; where &WhereStmnt. ; by &ByVar. ; tables &Var. /missing; ods output onewayfreqs=&var.f; data &Var.&ByVar. ; length Stat $100 Variable $32 Label $100 Level $50; set &Var.F; Stat = compress(frequency) " (" compress(round(percent,.01 )) ")" ; Variable = "&Var." ; Level = F_&Var. ; Label = vlabel(&var.); keep &ByVar. Level Label variable Stat; %Mend NPct; 24

Call macro %NPct(StatData=BWeight, WhereStmnt=, ByVar=Married, Var=black); %NPct(StatData=BWeight, WhereStmnt=, ByVar=Married, Var=boy); %NPct(StatData=BWeight, WhereStmnt=, ByVar=Married, Var=smoke); 25

Create one table with all output data Table1; set blackmarried boymarried smokemarried; 26

Table1 dataset looks like this: 27

Which you can turn into a report like this: 28

Macro Libraries Michael Winter Associate Director of Statistical Programming, Data Coordinating Center 29

Macro Libraries Macro libraries are a way to control and manage macros Especially useful when a group of programmers work together And they aren t very complicated! 30

Storing Compiled Macros Need to include a LIBNAME statement identifying where to store the macros, and some options: Libname DCCMac "\\ad.bu.edu\bumcfiles\sph\dcc\dept\dccsug\dcc Macros"; Options Mstored SASmStore = DCCMac McompileNote = all; SASmStore: Identifies the libname where macros are stored. Mstored: Indicates that stored macros are available in the location specified by SASmStore. McomplileNote = all: Writes a note to the log when compile is successful. 31

Example Storing Compiled Macro Using the macro Clara just defined, we just need to add some code to store it in the macro library: %Macro NPct(StatData=, WhereStmnt=, ByVar=, Var= )/store des = "Create Dataset Containing N Pct for 'Var'"; /store stores the macro, des= adds a description proc freq data=&statdata. ; where &WhereStmnt. ; by &ByVar. ; tables &Var. /missing; ods output onewayfreqs=&var.f; data &Var.&ByVar. ; length Stat $100 Variable $32 Label $100 Level $50; set &Var.F; Stat = compress(frequency) " (" compress(round(percent,.01 )) ")" ; Variable = "&Var." ; Level = F_&Var. ; Label = vlabel(&var.); keep &ByVar. Level Label variable Stat; %Mend NPct; 32

SAS Log 33

Storing Macros Be sure to save your source code (SAS program) with the macro definition you cannot re-create it from the compiled macro! Document the code well! 34

Viewing Stored Macros You can view a list of stored macros in a macro catalog using the following code: Libname DCCMac "\\ad.bu.edu\bumcfiles\sph\dcc\dept\dccsug\dcc Macros"; Options Mstored SASmStore = DCCMac McompileNote = all; proc catalog catalog=dccmac.sasmacr; contents; quit; 35

List of Stored Macros 36

Calling a Stored Macro Libname DCCMac "\\ad.bu.edu\bumcfiles\sph\dcc\dept\dccsug\dcc Macros"; Options Mstored SASmStore = DCCMac McompileNote = all; %NPct(StatData=BWeight, WhereStmnt=, ByVar=Married, Var=black); %NPct(StatData=BWeight, WhereStmnt=, ByVar=Married, Var=boy); %NPct(StatData=BWeight, WhereStmnt=, ByVar=Married, Var=smoke); 37