More About SAS Macros

Similar documents
Acknowledgments xi Preface xiii About the Author xv About This Book xvii New in the Macro Language xxi

SAS Macro. SAS Training Courses. Amadeus Software Ltd

SAS Macro Language: Reference

Introduction. Getting Started with the Macro Facility CHAPTER 1

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

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

Base and Advance SAS

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

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

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

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

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

Introduction to the SAS Macro Facility

MOBILE MACROS GET UP TO SPEED SOMEWHERE NEW FAST Author: Patricia Hettinger, Data Analyst Consultant Oakbrook Terrace, IL

Macro Basics. Introduction. Defining and Using Macro Variables. Defining and Using Macros. Macro Parameters. Part 1. Chapter 1. Chapter 2.

Program Validation: Logging the Log

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

SAS Macro Programming for Beginners

Tales from the Help Desk 5: Yet More Solutions for Common SAS Mistakes Bruce Gilsen, Federal Reserve Board

SAS Macro Language 1: Essentials. Course Notes

Unlock SAS Code Automation with the Power of Macros

BASICS BEFORE STARTING SAS DATAWAREHOSING Concepts What is ETL ETL Concepts What is OLAP SAS. What is SAS History of SAS Modules available SAS

SAS CURRICULUM. BASE SAS Introduction

Paper 149 JAY A. JAFFE

Seminar Series: CTSI Presents

title1 "Visits at &string1"; proc print data=hospitalvisits; where sitecode="&string1";

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

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

A Time Saver for All: A SAS Toolbox Philip Jou, Baylor University, Waco, TX

Macro Bugs - How to Create, Avoid and Destroy Them Ian Whitlock, Kennett Square, PA

Using Macro Functions

An Introduction to Macros Deb Cassidy

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

SAS Macro Design Issues Ian Whitlock, Westat

Understanding the Concepts and Features of Macro Programming 1

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

Leave Your Bad Code Behind: 50 Ways to Make Your SAS Code Execute More Efficiently.

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

Job Security: Using the SAS Macro Language to Full Advantage

SURVIVING THE SAS MACRO JUNGLE BY USING YOUR OWN PROGRAMMING TOOLKIT

CHAPTER 7 Using Other SAS Software Products

Jeff Phillips, ARC Professional Services Group Veronica Walgamotte, ARC Professional Services Group Derek Drummond, ARC Professional Services Group

Submitting SAS Code On The Side

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

Basic Macro Processing Prepared by Destiny Corporation

STATION

A Tutorial on the SAS Macro Language

Macros from Beginning to Mend A Simple and Practical Approach to the SAS Macro Facility

Introduction. Keeping Efficiency in Perspective. CHAPTER 11 Writing Efficient and Portable Macros

A Comparison of the LUA Procedure and the SAS Macro Facility

Functions vs. Macros: A Comparison and Summary

Sandra Hendren Health Data Institute

Reading and Writing Data from Microsoft Excel/Word Using DDE

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

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Macro Magic III. SNUG Presentation (30mins)

An Introduction to SAS Macros

From Carpenter's Complete Guide to the SAS Macro Language, Third Edition. Full book available for purchase here.

APPENDIX 4 Migrating from QMF to SAS/ ASSIST Software. Each of these steps can be executed independently.

PLA YING WITH MACROS: TAKE THE WORK OUT OF LEARNING TO DO MACROS. Arthur L. Carpenter

Macro Quoting: Which Function Should We Use? Pengfei Guo, MSD R&D (China) Co., Ltd., Shanghai, China

<:ards,. The SAS" Macro: An Aid to the User 3' pfbl:me~~&j1tbc(lpt; 2435 procopt; RESOLVED RESOLVED

A Mass Symphony: Directing the Program Logs, Lists, and Outputs

An Animated Guide: An Introduction to SAS Macro Quoting Russ Lavery Bryn Mawr, PA

Lecture 2: C Programming Basic

An Introduction to SAS Macros Steven First, Systems Seminar Consultants, Madison, WI

New Macro Features Added in SAS 9.3 and SAS 9.4

Your Own SAS Macros Are as Powerful as You Are Ingenious

Creating and Executing Stored Compiled DATA Step Programs

Journey to the center of the earth Deep understanding of SAS language processing mechanism Di Chen, SAS Beijing R&D, Beijing, China

Efficiency Programming with Macro Variable Arrays

COP4020 Programming Assignment 1 - Spring 2011

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

STAT:5400 Computing in Statistics. Other software packages. Microsoft Excel spreadsheet very convenient for entering data in flatfile

SAS Data Integration Studio Take Control with Conditional & Looping Transformations

Techniques for Writing Robust SAS Macros. Martin Gregory. PhUSE Annual Conference, Oct 2009, Basel

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

SAS Macros Advanced Techniques

Contents. About This Book...1

Macro Internals for the User Developer s Overview. Susan O Connor, SAS Institute Inc., Cary, NC

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

Developing Data-Driven SAS Programs Using Proc Contents

No, it's not magic: De-mystifying the SAS Macro Facility Jay A. Jaffe, PhD

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

SAS Macro Programming Tips and Techniques

The SAS Interface to REXX

SAS 9 Programming Enhancements Marje Fecht, Prowerk Consulting Ltd Mississauga, Ontario, Canada

Sample Questions. SAS Advanced Programming for SAS 9. Question 1. Question 2

David S. Septoff Fidia Pharmaceutical Corporation

SAS Windowing environment Tips and Tricks

Implementing User-Friendly Macro Systems Ed Heaton Data & Analytic Solutions, Inc. Sarah Woodruff Westat

SAS Programming Techniques for Manipulating Metadata on the Database Level Chris Speck, PAREXEL International, Durham, NC

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

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

C introduction: part 1

Better Metadata Through SAS II: %SYSFUNC, PROC DATASETS, and Dictionary Tables

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Project 2: Scheme Interpreter

Document and Enhance Your SAS Code, Data Sets, and Catalogs with SAS Functions, Macros, and SAS Metadata. Louise S. Hadden. Abt Associates Inc.

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Transcription:

More About SAS Macros (Than You Thought Possible) Donald P. Gallogly DCBS IMD

Topics The SAS Macros System Macro Variables Writing Macros

The SAS Macros System

The SAS Macros System SAS macros and macro variables are input to the SAS Macro System. SAS Macro System is a SAS-code generator. Output from the system is SAS code.

The SAS Macros System SAS program code submitted for execution Macro System Macro statements executed Macro variable references resolved Contains macro references (& or %)? Yes Macro facility invoked No Step is compiled or parsed Step is executed

The SAS Macros System data &data (drop=lower_bound upper_bound %if &byvar = dropme %then dropme;);

The SAS Macros System data basket (drop=lower_bound upper_bound); or data basket (drop=lower_bound upper_bound dropme);

MPRINT and MFILE MPRINT and MFILE are system options that allow you to see the SAS code that the macro system generates. These are very helpful when debugging macros. MPRINT causes resolved SAS code to be written to the log. MFILE causes SAS code to be written to an external file.

MPRINT and MFILE options mprint; filename mprint /raprod/home/coolra/mycode.sas ; options mprint mfile;

The SAS Macros System You can NEVER execute a macro statement from a data step conditional statement. data _null_; set mydata; if myvar = 1 then %let mymacrovar = TRUE; run;

The SAS Macros System You can NEVER execute a macro statement from a data step conditional statement. data _null_; run; set mydata; if myvar = 1 then The macro system executes the %let statement and leaves nothing behind.

SAS Macro Variables

Properties of SAS Macro Variables The value of the macro variable is TEXT. The macro processor will replace the macro reference with the value before executing macro statements or compiling SAS code.

Defining SAS Macro Variables Keyword %let followed by the variable name Followed by an equal sign, some text and a semicolon. Thus, %let YEAR = 2007;

Referencing SAS Macro Variables To use (reference) a SAS macro variable, use the & followed by the variable name, if &YEAR = 2007 then do;

SAS Macro Variables Not data set variables Independent of a SAS data set Contain a single value that remains constant unless explicitly changed Can contain nothing (unlike data set variables) Can contain up to 64k of text (about 12 pages) Can t contain control characters (,, &, ;)

SAS Macro Variables %let nothing =; %let y = mx + b; %let service_cat = E&M; %let division = Workers Compensation; %let debug = *; %let state = OR;

SAS Macro Variables %let state = WA; data _null_; set states_data; if &state = CA then do; end; run; %let state = OR; data _null_; set states_data; if &state = CA then do; end; run;

SAS Macro Variables User-Defined Macro Variables Defined by the user Replaced by their contents at compile-time Contain data of special interest to the user Automatic Macro Variables Defined automatically Also replaced by contents at compile-time Contain data about the state of the system

Useful Automatic Macro Variables &sysdate date that session began (date7.) &sysdate9 date that session began (date9.) &syslast name of the last data set modified (lib.name) &sysdsn name of the last data set modified (lib name) &sysuserid ID of the user who submitted the job &sysmacroname contains the name of the macro that is currently running

symputx Data step function to store values into a macro variable. call symputx(macro_var, value) data _null_; set mydata; if myvar = 1 then call symputx(macro_var,true); run;

symget Data step function that converts macro variable values to character strings that can be assigned to data set variables. Use it when macro variable name needs to be resolved during data step execution. data _null_; set mydata; myvar = &mymacro_var; run;

symget Data step function that converts macro variable values to character strings that can be assigned to data set variables. Use it when macro variable name needs to be resolved during data step execution. data _null_; set mydata; if servcat = surg then charge = rvu * &cfsurg; else if servcat = EandM then charge = rvu * &cfeandm; else if servcat = pharm then charge = rvu * &cfpharm; else if servcat = radio then charge = rvu * &cfradio; run;

symget Data step function that converts macro variable values to character strings that can be assigned to data set variables. Used when macro variable name needs to be resolved during data step execution. data _null_; set mydata; charge = rvu * symget( cf servcat); run;

SAS Macros

SAS Macros Allow you to write dynamic code. Allow you to conditionally execute proc and data steps. Allow you to create libraries of useful reusable functions.

SAS Macros by Code Substitution %Macro OpenSSXP; filename tab1 "/raprod/wcd/projects/temp&sysuserid..xls"; ods tagsets.excelxp options(embedded_titles='yes suppress_bylines='yes') file=tab1; %Mend OpenSSXP;

Passing Parameters Positional Parameters: must be listed in order. %macro ApplyPharmMult(PharmData, Year); %ApplyPharmMult(PhDat, 2006); Keyword Parameters: must be referred to by keyword. %macro TrimOutliers(data=, byvar=dropme, datavar=); %TrimOutliers (byvar=service_code, data=basket, datavar=payment);

Passing Parameters Refer to parameters within the macro as you would any macro variables. %macro TrimOutliers(data=, byvar=dropme, datavar=); %if &byvar eq dropme %then %do; %end; data &data; run; %mend TrimOutliers; set &data; dropme = 1;

Conditional Macro Statements Conditional statements make decisions based on current conditions. %macro TrimOutliers(data=, byvar=dropme, datavar=); %if &byvar eq dropme %then %do; data &data; set &data; dropme = 1; run; %end; %mend TrimOutliers;

%Do blocks %Do blocks allow you to repeat sections of code repeatedly. data thresholds (keep=&byvar upper_bound lower_bound); set percentiles; %do i=90 %to 99; if P%eval(&i+1)/P&i gt 1.5 then upper_bound = P&i*1.2; else %end; upper_bound = P&i; run;

%Do blocks %Do blocks allow you to repeat sections of code repeatedly. data thresholds (keep=servcat upper_bound lower_bound); set percentiles; if P91/P90 gt 1.5 then upper_bound = P90*1.2; else if P92/P91 gt 1.5 then upper_bound = P91*1.2; else if P93/P92 gt 1.5 then upper_bound = P92*1.2; else if P94/P93 gt 1.5 then upper_bound = P93*1.2; else if P95/P94 gt 1.5 then upper_bound = P94*1.2; else if P96/P95 gt 1.5 then upper_bound = P95*1.2; else if P97/P96 gt 1.5 then upper_bound = P96*1.2; else if P98/P97 gt 1.5 then upper_bound = P97*1.2; else if P99/P98 gt 1.5 then upper_bound = P98*1.2; else if P100/P99 gt 1.5 then upper_bound = P99*1.2; else upper_bound = P100; run;

Writing to the log %Put writes to the log from the macro processor just as Put does at run-time. %put &sysdsn displays the name of last modified dataset.

Writing to the log %Put writes to the log from the macro processor just as Put does at run-time. %put &sysdsn displays the name of last modified dataset. %put ERROR: File not found. %put WARNING: Data out of range. %put NOTE: Have a nice day.

Text functions %index(arg1, arg2); %length(arg); %scan & %qscan(arg, n); %substr & %qsubstr(arg, pos, <len>); %upcase & %qupcase(arg); Returns the position of arg2 in arg1. Returns the length of arg. Returns the nth word in arg1. Returns a string of length len, starting at position pos, from arg. Returns arg in ALL CAPS. Use the Q(uoted) functions when arg might include quotes, ampersands, percent signs, etc.

Mathematical Functions %eval and %sysevalf do arithmetic using macro variables and return the result. %eval only does integer arithmetic. %sysevalf only does floating point arithmetic.

Mathematical Functions %macro line; %let m = 0.5; %let b = 2; %do x = 0 to 100; %let y = &m * &x + &b; %put y = &y; %end; %mend line;

Mathematical Functions y = 0.5 * 0 + 2 y = 0.5 * 1 + 2 y = 0.5 * 2 + 2 y = 0.5 * 3 + 2 y = 0.5 * 4 + 2 y = 0.5 * 5 + 2 y = 0.5 * 6 + 2 y = 0.5 * 7 + 2 y = 0.5 * 100 + 2

Mathematical Functions %macro line; %let m = 0.5; %let b = 2; %do x = 0 to 100; %let y = %sysevalf(&m*&x + &b); %put y = &y; %end; %mend line;

Mathematical Functions y = 2.0 y = 2.5 y = 3.0 y = 3.5 y = 4.0 y = 4.5 y = 5.0 y = 5.5 y = 52.0

Using data step functions and CALL routines %syscall and %sysfunc can be used to call routines and functions that are normally only available in the data step.

Using CALL Routines %syscall executes CALL routines. %let seed = 9876; %let random_num = ; %syscall ranuni(seed, random_num); %put Your pseudorandom number is &random_num; Your pseudorandom number is 0.9876783463462

Using data step functions %sysfunc executes data step functions. %if %sysfunc(abs(payment)) > %sysfunc(abs(charge)) %then %do; %put Today is &sysfunc(date(), date7.);

Q & A Time Thank you for coming