Macro Overview Mihaela Simion. Macro Facility Overview Definition : The SAS Macro Facility is a tool within base SAS software that contains the essential.

Slides:



Advertisements
Similar presentations
The INFILE Statement Reading files into SAS from an outside source: A Very Useful Tool!
Advertisements

Examples from SAS Functions by Example Ron Cody
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.
SYSTEM PROGRAMMING & SYSTEM ADMINISTRATION
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
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Basic And Advanced SAS Programming
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Let SAS Do the Coding for You! Robert Williams Business Info Analyst Sr. WellPoint Inc.
How to Build Tabular Dashboards Using Proc Report
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
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.
Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute.
SAS Macros ® 101 How I learned to stop worrying and love macros Alex Chaplin BCS USA Section.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Managing Passwords in the SAS System Allen Malone Senior Analyst/Programmer Kaiser Permanente.
Topics Introduction Hardware and Software How Computers Store Data
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
An Animated Guide©: Sending SAS files to Excel Concentrating on a D.D.E. Macro.
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.
BMTRY 789 Introduction to SAS Programming Lecturer: Annie N. Simpson, MSc.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Tutorial 10 Programming with JavaScript
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.
Introduction to SAS/Graph 9.2 Ken Barz Colorado Prevention Center 22Oct2009 Ken Barz Colorado Prevention Center.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
Publishing to PDF SNUG Quarter 2. Overview n What is PDF? n Why use PDF? n Creating PDF files with SAS Software n Issues n Advanced PDF files with SAS.
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Priya Ramaswami Janssen R&D US. Advantages of PROC REPORT -Very powerful -Perform lists, subsets, statistics, computations, formatting within one procedure.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
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.
While You Were Sleeping… SAS Is Hard At Work Andrea Wainwright- Zimmerman.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
FORMAT statements can be used to change the look of your output –if FORMAT is in the DATA step, then the formats are permanent and stored with the dataset.
1 Chapter 3: Getting Started with Tasks 3.1 Introduction to Task Dialogs 3.2 Creating a Listing Report 3.3 Creating a Frequency Report 3.4 Creating a Two-Way.
Customize SAS Output Using ODS Joan Dong. The Output Delivery System (ODS) gives you greater flexibility in generating, storing, and reproducing SAS procedure.
Chapter – 8 Software Tools.
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Online Programming| Online Training| Real Time Projects | Certifications |Online Classes| Corporate Training |Jobs| CONTACT US: STANSYS SOFTWARE SOLUTIONS.
Chapter 1: Introduction to Computers and Programming.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
Use SAS to Automate Hospital Reports generation Cardiac Service BC Stats Team, PHSA Tina Yang.
HTML LAYOUTS. CONTENTS Layouts Example Layout Using Element Example Using Table Example Output Summary Exercise.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Fall 2017 Questions and Answers (Q&A)
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 ©
Exploring Microsoft Excel
Defining and Calling a Macro
3 Iterative Processing.
Automating SAS through the Power of VB Script
Purpose Real life scenario: I have a set of reports to refresh every month but data is only available at any day during Day 3 – 5 at beginning of each.
Tips and Tricks for Using Macros to Automate SAS Reporting.
Presentation transcript:

Macro Overview Mihaela Simion

Macro Facility Overview Definition : The SAS Macro Facility is a tool within base SAS software that contains the essential elements which enables you to use the macros.

Properties of the MF The MF contains a macro processor that translate macro code into statements which can be used by SAS System, and the macro language. The macro language ML provides the means to communicate with the macro processor. The macro language consists of its own set of commands, options, syntax and compiler. There are syntax differences between the SAS Macro and SAS Base.

Benefits of using the MF The Macro language provides tools that: Pass information between SAS steps. Dynamically create code at the execution time. Conditionally execute DATA or PROC steps. Create generalization and flexible code.

A Program Example This program is a great example because will show you how some of this tools are working, in order to access MF. Using Macro variable and subroutines I was able to provide a great automatization to a long process, full of many repetitive tasks. The objective was to provide multiple reports (<50) to different branch managers by or saving them in a share drive.

Options options nomprint nomlogic nosymbolgen; *options mprint mlogic symbolgen; Debugging a macro can be, under the best of conditions, difficult. You could use these two options statement at the beginning of the program, that contains: MPRINT= displays the test or SAS statements that are generated by macro executions, one statement per line, with macro variable references solved. MLOGIC=traces the macro logic and follows the pattern of execution. SYMBOLGEN=print a message in the LOG whenever a macro variable is resolved.

Step 1 data _null_; format yer yer1 4. mth mth1 2.; dat=“&sysdate”d; mth =month(dat); yer=year(dat); if mth=1 then do; yer=yer-1; mth=12; end; else do; mth1=mth-1; yer1=yer; end; dat2=mdy(mth,1,yer1); call symput(‘prevmth’,put(dat2,monyy5. )); run; Create a Macro variable for the previous month date using a data _null_ step. In this step we use one of the most important macro function: &sysdate In order to create the macro variable I called the symput subroutine: call symput(‘prevmth’,put(dat2,mont hyy7.)); The macro variable prevmth, that will be solved like simple text will be used in the title of the report.

Log options mprint mlogic symbolgen; data _null_; 106 format yer yer1 4. mth mth1 2.; 107 dat="&sysdate"d; SYMBOLGEN: Macro variable SYSDATE resolves to 15SEP mth =month(dat); 109 yer=year(dat); 110 if mth=1 then do; 111 yer=yer-1; 112 mth=12; 113 end; 114 else do; 115 mth1=mth-1; 116 yer1=yer; 117 end; 118 dat2=mdy(mth,1,yer1); 119 call symput('prevmth',put(dat2,monyy5.)); 120 run; NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds

Step2 %macro totrep(data1,list,var1); Open the macro function totrep (data1. list, var1) where: data1=input data set. list =the list of the control parameters. By example in my case was transit#, district, region. They could be used for the sorting purpose. Could be missed when it is not need for special sorting. Var1= the variable used for the creation of the reports (In my example was transit#). When there are multiple parameters, the comma separates their values.

Step2 proc sort data=&data1: by &var1; run; data _null_; set &data1: by &var1; if first..&var1 then do: i+1; ii=left(put(i,2)); call symput( ‘var1a’||ii,put(&var1,4.))); end; cal symput( ‘total’,ii); run; Create a macro variable for each value of the variable var1 :var1a1,var1a2,… call symput(‘var1a’||ii,put(&var1,4.)) ); Create a macro variable for the number of the reports we want to edit, named: total Observe how is used the first variable in the if statement: double dots if first..&var1 then do. For the efficiency purpose I limited the number of the created macro variables to 50.

Step 3 %do i=1 %to %total: proc sort data=&data1: by &list; run; data report ; set &data1: if var1 in (&&var1a&i); call symput(“var1b”,put( left(&var1),4.)); run; Create the loop that will create for each I to the total( number of values for the variable var1) In the first part of the loop the data was sorted by list.We sort by list if it is another order needed. Second part of the loop is a data step that creates a temporary dataset name report and a macro variable: var1b, used in the name of excel report.

A Macro Exercise ods listing close ; filename result1 “H:\temp\audit&var1b..xls”; ods html body=results1; proc report data=report nowindows headline headskip split=‘*’ misssing out=result1; style( report)={ Background =white} style(header)= {foreground =dark blue background =whitte font_weight=bold font_size=1} Style( column ) = { background=white “ region regname “ dis_name regname “ trn_name $28.; endcomp: compute after nat; 9 Lnamount.sum dollar prbalos.sum doll1ar1.; endcomp; title1;title2;title3; title4 “Monthly Report as at “&prevmth”; footnote1; footnote2 “The Report is addressed to the to the manager of the branch &var1b”; We are still in the macro torep I open an ods (output delivery system) to build the report in html format or an excel file.

A Macro Exercise ods html close;run; file name mymail1 “&var1”; subject =“Monthly report for &var1” attach =‘H:\temp\audit& var1b..xls”; data _null_; put mymail1; put ; put “This is the monthly report for..”;run; %end; %mend totrep; Close the ods for the report. Send the , inclosing the report. Close the loop %end; Close the macro %mend ;

A Macro Exercise Calling the macro: %totrep(data1=plcdata, list= region district transit,var1=transit); or %totrep(plcdata,region district transit,transit); If we have an enumeration in one of the parameters in the macro don’t use the comma between the components. See the list enumeration in the example.

Some suggestions for improvements and application The proc report could be changed with PROC TEMPLATE. You could create a list with s and transit numbers to be sent. You could also use a data set containing the data and the months that you could use instead of the previous month prevmth macro variable.

Bibliography 1. Carpenter’s Complete Guide to the SAS Macro Language. 2. Advanced Macro Languages-SAS Institute.

Have fun!