Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71.
 2005 Pearson Education, Inc. All rights reserved Introduction.
VBA Modules, Functions, Variables, and Constants
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
“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.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute.
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.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1 Chapter 1: Introduction 1.1 Course Logistics 1.2 Purpose of the Macro Facility 1.3 Program Flow.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
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
1 Chapter 2: Macro Variables 2.1 Introduction to Macro Variables 2.2 Automatic Macro Variables 2.3 Macro Variable References 2.4 User-Defined Macro Variables.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
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.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Chapter 21: Controlling Data Storage Space 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
Chapter 14: Combining Data Vertically 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
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.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 5: Passing and Processing Macro Parameters
Chapter 6: Modifying and Combining Data Sets
Two “identical” programs
Microsoft Access Illustrated
Chapter 18: Modifying SAS Data Sets and Tracking Changes
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 ©
Chapter 24 (4th ed.): Creating Functions
Creating Macro Variables and Assigning Value
WEB PROGRAMMING JavaScript.
Creating Macro Variables in the DATA Step
Macro Variable’s scope
Defining and Calling a Macro
A Useful Footnote Martha Cox Population Health Research Unit Community Health & Epidemiology Dalhousie University Good morning. We've all had this happen:
How Best to Use Macro Quoting Functions?
CIS 136 Building Mobile Apps
Presentation transcript:

Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

Outline Automatic Macro Variables Automatic Macro Variables User-defined Macro Variables User-defined Macro Variables Processing Macro Variables Processing Macro Variables Displaying Macro Variables Displaying Macro Variables Masking Special Characters Masking Special Characters Manipulating Character Strings Manipulating Character Strings SAS Functions and Macro Variables SAS Functions and Macro Variables 2

Macro Variables Macro variables allow the user Macro variables allow the user –to substitute text—particularly repetitive text –to obtain session information –to obtain information on text strings 3

Macro Variables-%LET SAS programs often include a single variable used and defined in multiple locations SAS programs often include a single variable used and defined in multiple locations %LET allows the user to define a macro variable, often at the start of the program, and substitute the macro variable throughout the program %LET allows the user to define a macro variable, often at the start of the program, and substitute the macro variable throughout the program 4

Macro Variables-%LET Original code Original code title " E Coli Data for 2010 " ; data perm.ecoli2010; set ecoli2010; if year(collection_dat e)=2010; run; 5 Modified code %let year=2010; title " E Coli Data for &year " ; data perm.ecoli&year; set ecoli&year; if year(collection_date) =&year ;

Macro Variables SAS’s macro facility allows text to be saved as macro variables Macro variables are independent of SAS data sets Two types of macro variables – –automatic – –user-defined

Macro Variables The value of a macro variable is stored in a symbol table Automatic macro variables are always available in the global symbol table As you saw from the earlier example, macro variables are referenced by preceding their name with a &

Macro Variables – –The macro processor searches symbol tables for a referenced macro variable – –A reference cannot be identified if it is placed within single quotes; double quotes must be used instead – –A message will be printed in the SAS log when macro variable references cannot be resolved

Macro Variables %let year=2010; title “E Coli Data for &year”; data perm.ecoli&year; set ecoli&year; if year(collection_date)=&year; proc print data=perm.ecoli&year (obs=10); run;

Automatic Macro Variables Automatic Macro Variables are created when a new SAS session starts As mentioned before, they are global and typically assigned values by SAS Users may be able to re-assign values in some cases

Automatic Macro Variables The most common automatic variables reference the current date, day, or time, the current version of SAS or the current SAS data set

Automatic Macro Variables title “Yacht Rentals”; title2 “Data from &SYSLAST”; footnote “Created &systime &sysday, &sysdate9”; footnote2 “on &sysscp system using Release &sysver”; footnote3 “by User &sysuserid”;

Automatic Macro Variables proc tabulate data=boats format=dollar9.2; class locomotion type; var price; table type, mean=type*price; run;

User-defined macro variables %LET is the most common method to assign a value (right side of statement) to your own macro variable (left side of statement) – – Values are stored as character strings – – Quotation marks are stored as part of the value

User-defined macro variables %let month=JAN; title "E Coli Data for &month 2009"; data perm.ecoli&month; set perm.ecoli; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month"; proc print data=perm.ecoli&month (obs=10); run;

Processing Macro Variables   Processing macro variables takes place within SAS’s general text processing:   Program is sent to the input stack   Code is sent to compiler until the end of a step   Compiler executes the code

Processing Macro Variables   SAS parses (or tokenizes) the code in the input stack and passes the tokens to the compiler a statement at a time   Useful in understanding difficulties that arise in resolving macro references

Processing Macro Variables   Tokens are   Quoted strings   Numbers   Names (SAS commands, infiles, variables,..)   Special characters (*, &, ;,..)

Processing Macro Variables Example: sx=sum(of x1-x4); The 10 tokens are: sx = sum ( of x1 – x4 ) ;

Processing Macro Variables   Code is sent to the macro processor when particular token sequences occur   The macro triggers are what you would expect - -% immediately followed by a name token - -& immediately followed by a name token   Macro variables are created/updated in the symbol table then sent to the input stack and tokenized

Displaying Macro Variables   You can display macro variables in the Log window using either options symbolgen; or %put   %put allows you to print text to the log, as well as macro variables

Masking Special Characters   SAS has several characters that can make complex macro variables difficult to print   There are a couple different ways to handle these difficulties – –%STR and %NRSTR – –%BQUOTE

Masking Special Characters Two methods to print a macro variable that is a sequence of SAS steps options symbolgen; %let demo=%str(data a; set b; run;); %let demo=data a%str(;) set b%str(;) run(%str);

Masking Special Characters The % sign can be used within the %str argument to print single quotes embedded in a title. %options symbolgen; %let text=%str(Today%’s Weather);

Masking Special Characters %nrstr() works in the same way as %str(), but can also mask macro characters % and & options symbolgen; %let cite=%nrstr( (Grego, Li, Lynch & Sethuraman, 2012)); %put cite is interpreted as &cite;

Masking Special Characters  %bquote() ignores special characters during macro compilation and resolves them during execution  It’s more user-friendly than %str

Manipulating Character Strings   Macro character functions are obvious analogs to SAS character functions, but designed to work with macro variables as character strings   Some of these work with − −%upcase, %substr, %index, %scan, %cmpres − −%qupcase, etc works similarly to %bquote

SAS Functions and Macro Variables   %SYSFUNC is a powerful command that allows you to introduce standard SAS functions in the macro environment   Only a limited number of SAS functions are unavailable for use

Macro Variables and text   We have already seen several instances of macro variables combined with text   E.g: data ecoli&month&year;   SAS may have difficulty resolving some references, but these can be resolved by adding a delimiter to the end of a macro variable name