USING DATA TO SET MACRO PARAMETERS

Similar documents
Formats and the Format Procedure

ABSTRACT INTRODUCTION MACRO. Paper RF

More with SQL Queries. Advanced SAS Programming

Sign of the Times: Using SAS to Produce Conference Signage Daniel K. Downing, Caremark, Scottsdale, Ariz.

Base and Advance SAS

Essential ODS Techniques for Creating Reports in PDF Patrick Thornton, SRI International, Menlo Park, CA

A SAS Macro for Producing Benchmarks for Interpreting School Effect Sizes

There s No Such Thing as Normal Clinical Trials Data, or Is There? Daphne Ewing, Octagon Research Solutions, Inc., Wayne, PA

T.I.P.S. (Techniques and Information for Programming in SAS )

An Explicit-Continuation Metacircular Evaluator

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

Introduction to the SAS System

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

A Guided Tour Through the SAS Windowing Environment Casey Cantrell, Clarion Consulting, Los Angeles, CA

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

Lab #1: Introduction to Basic SAS Operations

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

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

A Format to Make the _TYPE_ Field of PROC MEANS Easier to Interpret Matt Pettis, Thomson West, Eagan, MN

A Macro to Manage Table Templates Mark Mihalyo, Community Care Behavioral Health Organization, Pittsburgh, PA

SAS Macro Technique for Embedding and Using Metadata in Web Pages. DataCeutics, Inc., Pottstown, PA

SAS CURRICULUM. BASE SAS Introduction

ABSTRACT MORE THAN SYNTAX ORGANIZE YOUR WORK THE SAS ENTERPRISE GUIDE PROJECT. Paper 50-30

Creating Macro Calls using Proc Freq

Copy That! Using SAS to Create Directories and Duplicate Files

Unlock SAS Code Automation with the Power of Macros

Developing Data-Driven SAS Programs Using Proc Contents

Identifying Duplicate Variables in a SAS Data Set

A Way to Work with Invoice Files in SAS

Taming a Spreadsheet Importation Monster

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

Christopher Louden University of Texas Health Science Center at San Antonio

Chapter 1. Introduction. 1.1 More about SQL More about This Book 5

SAS Online Training: Course contents: Agenda:

Introduction. Getting Started with the Macro Facility CHAPTER 1

3. Almost always use system options options compress =yes nocenter; /* mostly use */ options ps=9999 ls=200;

SAS Macro Language: Reference

A Quick and Gentle Introduction to PROC SQL

My Reporting Requires a Full Staff Help!

SAS Macro Programming for Beginners

An Introduction to SAS Macros

Programming Gems that are worth learning SQL for! Pamela L. Reading, Rho, Inc., Chapel Hill, NC

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

A Cross-national Comparison Using Stacked Data

Data Quality Review for Missing Values and Outliers

Checking for Duplicates Wendi L. Wright

SAS Programming Basics

Common Sense Validation Using SAS

SD10 A SAS MACRO FOR PERFORMING BACKWARD SELECTION IN PROC SURVEYREG

Using PROC SQL to Generate Shift Tables More Efficiently

My SAS Grid Scheduler

Statements with the Same Function in Multiple Procedures

MarkMagic 6 Bar Code Labels, RFID Tags, and Electronic Forms Software for IBM System i

ABSTRACT DATA CLARIFCIATION FORM TRACKING ORACLE TABLE INTRODUCTION REVIEW QUALITY CHECKS

Summarizing Impossibly Large SAS Data Sets For the Data Warehouse Server Using Horizontal Summarization

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Efficient Processing of Long Lists of Variable Names

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

Efficiency Programming with Macro Variable Arrays

footnote1 height=8pt j=l "(Rev. &sysdate)" j=c "{\b\ Page}{\field{\*\fldinst {\b\i PAGE}}}";

PhUSE US Connect 2018 Paper CT06 A Macro Tool to Find and/or Split Variable Text String Greater Than 200 Characters for Regulatory Submission Datasets

QUERIES BY ODS BEGINNERS. Varsha C. Shah, Dept. of Biostatistics, UNC-CH, Chapel Hill, NC

Compute Blocks in Report

Posters. Paper

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED)

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

Part 1. Getting Started. Chapter 1 Creating a Simple Report 3. Chapter 2 PROC REPORT: An Introduction 13. Chapter 3 Creating Breaks 57

Extending the Scope of Custom Transformations

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

A simplistic approach to Grid Computing Edmonton SAS Users Group. April 5, 2016 Bill Benson, Enterprise Data Scienc ATB Financial

Sandra Hendren Health Data Institute

22S:172. Duplicates. may need to check for either duplicate ID codes or duplicate observations duplicate observations should just be eliminated

SAS Macro. SAS Training Courses. Amadeus Software Ltd

EXAMPLE 2: INTRODUCTION TO SAS AND SOME NOTES ON HOUSEKEEPING PART II - MATCHING DATA FROM RESPONDENTS AT 2 WAVES INTO WIDE FORMAT

The REPORT Procedure CHAPTER 32

Seminar Series: CTSI Presents

Getting it Done with PROC TABULATE

A SAS Macro to Generate Caterpillar Plots. Guochen Song, i3 Statprobe, Cary, NC

Programming & Manipulation. Danger: MERGE Ahead! Warning: BY Variable with Multiple Lengths! Bob Virgile Robert Virgile Associates, Inc.

CC13 An Automatic Process to Compare Files. Simon Lin, Merck & Co., Inc., Rahway, NJ Huei-Ling Chen, Merck & Co., Inc., Rahway, NJ

Tweaking your tables: Suppressing superfluous subtotals in PROC TABULATE

ODS DOCUMENT, a practical example. Ruurd Bennink, OCS Consulting B.V., s-hertogenbosch, the Netherlands

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

CHAPTER 7 Using Other SAS Software Products

Pros and Cons of Interactive SAS Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

PharmaSUG China Paper 059

The Basics of PROC FCMP. Dachao Liu Northwestern Universtiy Chicago

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

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

Useful Tips When Deploying SAS Code in a Production Environment

How to Keep Multiple Formats in One Variable after Transpose Mindy Wang

Correcting for natural time lag bias in non-participants in pre-post intervention evaluation studies

SAS Display Manager Windows. For Windows

%GrabTweet: A SAS Macro to Read JSON Formatted Tweets

UAccess ANALYTICS Next Steps: Working with Bins, Groups, and Calculated Items: Combining Data Your Way

Processing SAS Data Sets

Fly over, drill down, and explore

Paper B GENERATING A DATASET COMPRISED OF CUSTOM FORMAT DETAILS

KEYWORDS Metadata, macro language, CALL EXECUTE, %NRSTR, %TSLIT

A Few Quick and Efficient Ways to Compare Data

Transcription:

USING DATA TO SET MACRO PARAMETERS

UPDATE A PREVIOUS EXAMPLE %macro report(regs=); %let r=1; %let region=%scan(&regs,&r); %do %until(&region eq ); options nodate pageno=1; ods pdf file="&region..pdf"; Title "Job Cost Reporting for %sysfunc(propcase(&region)) Region"; proc report data=mysas.projects nowd; where upcase(region) eq "%upcase(&region)"; column pol_type equipmnt personel jobtotal; define pol_type/group 'Pollutant'; define equipmnt/mean 'Avg. Equipment Cost' format=dollar10.; define personel/mean 'Avg. Personnel Cost' format=dollar10.; define jobtotal/mean 'Avg. Total Cost' format=dollar10.; ods pdf close; %let r=%eval(&r+1); %let region=%scan(&regs,&r); %end; %mend;

UPDATE A PREVIOUS EXAMPLE Instead of having the user produce a list of regions for which reports will be generated, we will have our code determine which regions exist and create reports for all of them. Therefore, the information the macro will need to alter through its iterations are contained in the data set many times over, as will usually be the case. Achieving this can be thought of as having the following important characteristics: Need a procedure/method that can produce the set of unique values of the variable. These values may be intermediately stored in a data set for most methods this is required, for some special statements may be helpful. Those values must become values for macro variable(s). A few of our analysis/reporting procedures could be used in this case, none of which is likely easier than PROC FREQ

PULLING A SET OF VALUES FROM A DATA SET This code: proc freq data=mysas.projects; table region; quit; Produces the values we need (plus some we do not): But this is not data what statement can you add to make this a data table?

PULLING A SET OF VALUES FROM A DATA SET Another method you may be less familiar with: proc sort data=mysas.projects out=regions nodupkey; by region;

PULLING A SET OF VALUES FROM A DATA SET Another method you may be less familiar with: proc sort data=mysas.projects out=regions nodupkey; by region; nodupkey removes duplicates of the key value; which is the set of variables you sort by

PULLING A SET OF VALUES FROM A DATA SET Another method you may be less familiar with: proc sort data=mysas.projects out=regions nodupkey; by region; So the sorted data has only 6 observations, corresponding to the 6 different values of region in this case.

PULLING A SET OF VALUES FROM A DATA SET Another method you may be less familiar with: proc sort data=mysas.projects out=regions nodupkey; by region; So the sorted data has only 6 observations, corresponding to the 6 different values of region in this case.

PULLING A SET OF VALUES FROM A DATA SET Another method you may be less familiar with: proc sort data=mysas.projects out=regions nodupkey; by region; For either of these methods, a keep= option on the output data set can be used to limit this to only the values desired.

NOW WHAT? Putting these into a data set should allow for macro variable construction via the call symput routine. We could, using the catx function in the data step, assemble these into a list and then run the remainder of the code exactly as we did previously. But it would be silly to put them together only to immediately separate them again. In this case, we will create a set of macro variables for the set of regions, something like this: data _null_; set regions; call symput(cats('region',put(_n_,1.)),strip(region));

NOW WHAT? Putting these into a data set should allow for macro variable construction via the call symput routine. We could, using the catx function in the data step, assemble these into a list and then run the remainder of the code exactly as we did previously. But it would be silly to put them together only to immediately separate them again. In this case, we will create a set of macro variables for the set of regions, something like this: data _null_; set regions; call symput(cats('region',put(_n_,1.)),strip(region)); We create an indexed set of macro variables, as we have before. This time, the data step record counter, _n_, is used to create the numerical suffix.

A LITTLE BETTER Now a counter could be manually set up with a conditional loop, similar to the iteration from the previous example, but the information is available to tell the program exactly how many times to loop. Have the data step create one more macro variable: The end= variable helps determine when the end of the set is reached... data _null_; set regions end=final; call symput(cats('region',put(_n_,1.)),strip(region)); if final then do; call symput('num',put(_n_,1.)); end;

A LITTLE BETTER Now a counter could be manually set up with a conditional loop, similar to the iteration from the previous example, but the information is available to tell the program exactly how many times to loop. Have the data step create one more macro variable: data _null_; set regions end=final; call symput(cats('region',put(_n_,1.)),strip(region)); if final then do; call symput('num',put(_n_,1.)); end; So the value of the data step counter is now the length of the list.

THE ITERATIVE DO LOOP Since the number of iterations has been determined, conditional iteration is not necessary and an iterative loop can be employed. Macro do loop, iterative: %do var=exp1 %to exp2 %by exp3; %end; SAS statements Requirements and options: exp1, exp2 and exp3 must each evaluate to an integer (usually positive) The %by is optional, the default value is 1 var is referred to as the index variable and is a macro variable The loop iterates for all values from exp1 (inclusive) to exp2, as dictated by exp3 depending on the spacing of these values, there is no guarantee that the index variable will take on the value of exp2

FINISH So following the sort and data step, this loop completes the macro: %do j=1 %to # options nodate pageno=1; ods pdf file="&&region&j...pdf"; Title "Job Cost Reporting for %sysfunc(propcase(&&region&j)) Region"; proc report data=mysas.projects nowd; where region eq "&&region&j"; column pol_type equipmnt personel jobtotal; define pol_type/group 'Pollutant'; define equipmnt/mean 'Avg. Equipment Cost' format=dollar10.; define personel/mean 'Avg. Personnel Cost' format=dollar10.; define jobtotal/mean 'Avg. Total Cost' format=dollar10.; quit; ods pdf close; %end;

FINISH So following the sort and data step, this loop completes the macro: %do j=1 %to # %end; options nodate pageno=1; ods pdf file="&&region&j...pdf"; Title "Job Cost Reporting for %sysfunc(propcase(&&region&j)) Region"; proc report data=mysas.projects nowd; quit; ods pdf close; where region eq "&&region&j"; column pol_type equipmnt personel jobtotal; define pol_type/group 'Pollutant'; define equipmnt/mean 'Avg. Equipment Cost' format=dollar10.; define personel/mean 'Avg. Personnel Cost' format=dollar10.; define jobtotal/mean 'Avg. Total Cost' format=dollar10.; Since j is the chosen do loop index, all references to region are now of the indexed form &&region&j

FINISH So following the sort and data step, this loop completes the macro: %do j=1 %to # options nodate pageno=1; ods pdf file="&&region&j...pdf"; Why three dots? Title "Job Cost Reporting for %sysfunc(propcase(&&region&j)) Region"; proc report data=mysas.projects nowd; where region eq "&&region&j"; column pol_type equipmnt personel jobtotal; define pol_type/group 'Pollutant'; define equipmnt/mean 'Avg. Equipment Cost' format=dollar10.; define personel/mean 'Avg. Personnel Cost' format=dollar10.; define jobtotal/mean 'Avg. Total Cost' format=dollar10.; quit; ods pdf close; %end;

EXERCISE 1 Modify the previous so the user can choose whether they want reports for each region or each pollution type. The one not chosen should be the grouping variable in the report.

EXERCISE 2 For the data sets in the Monthly Data 4 folder, write a macro that will combine all of these data sets into a single data set (including a variable that indicates where the data originated from.