Creating Macro Variables in the DATA Step

Slides:



Advertisements
Similar presentations
Examples from SAS Functions by Example Ron Cody
Advertisements

Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 10.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Chapter 2 Basic Elements of Fortan
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
JavaScript, Third Edition
Basic And Advanced SAS Programming
Introduction to scripting
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
1 Chapter 3: Macro Definitions 3.1 Defining and Calling a Macro 3.2 Macro Parameters 3.3 Macro Storage (Self-Study)
© 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.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
1 Chapter 1: Introduction 1.1 Course Logistics 1.2 Purpose of the Macro Facility 1.3 Program Flow.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
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.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Macro Variable Resolution Enio Presutto York University, Toronto, Canada.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
1 Chapter 4: DATA Step and SQL Interfaces 4.1 Creating Macro Variables in the DATA Step 4.2 Indirect References to Macro Variables 4.3 Retrieving Macro.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
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.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture 3 Translation.
Chapter 5: Passing and Processing Macro Parameters
Chapter 6 JavaScript: Introduction to Scripting
Chapter 3 Assignment and Interactive Input.
A First Book of ANSI C Fourth Edition
Two “identical” programs
Intro to PHP & Variables
What is Bash Shell Scripting?
Creating Macro Variables in SQL (Review)
Conditional Processing
IDENTIFIERS CSC 111.
Topics Introduction to File Input and Output
3 Macro Parameters.
Number and String Operations
The Metacircular Evaluator
A First Book of ANSI C Fourth Edition
Indirect References to Macro Variables
Macro Variable’s scope
Defining and Calling a Macro
Global and Local Symbol Tables
Retrieving Macro Variables in the DATA Step
3 Iterative Processing.
An Introduction to Programming with C++ Fifth Edition
Topics Introduction to File Input and Output
Presentation transcript:

Creating Macro Variables in the DATA Step 4 Creating Macro Variables in the DATA Step

Create macro variables during DATA step execution Create macro variables during DATA step execution. Describe the difference between the SYMPUTX routine and the %LET statement.

The DATA Step Interface Automate production of the report below, with the footnote indicating the number of internet orders. Internet orders have an Order_Type of 3.

The DATA Step Interface, First Try %let month=2; %let year=2007; data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=&year and month(order_date)=&month; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; %let foot=No Internet Orders; end; else do; %let foot=Some Internet Orders; run; proc print data=orders; title "Orders for &month-&year"; footnote "&foot"; The DATA Step Interface, First Try This is a "decoy" program that does not work because, as learned in chapter one, macro language statements, such as %let, are routed to the macro processor, not the DATA step compiler, during word-scanning time. While the %let statement can "legally" be placed anywhere, placing it in a DATA step does not magically transform it into a DATA step statement. Same would be true of a title statement, which can also be placed anywhere, but can never be a DATA step statement.

The DATA Step Interface Symbol Table The DATA Step Interface month 2 year 2007 Word scanning begins. Macro triggers are encountered. %let month=2; %let year=2007; data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=&year and month(order_date)=&month; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; %let foot=No Internet Orders; end; else do; %let foot=Some Internet Orders; run; ...

The DATA Step Interface Compiling begins. Macro variable references are resolved. data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=2007 and month(order_date)=2; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; %let foot=No Internet Orders; end; else do; %let foot=Some Internet Orders; run; Symbol Table month 2 year 2007 ...

The DATA Step Interface Symbol Table The DATA Step Interface month 2 year 2007 foot No Internet Orders The macro trigger is passed to the macro processor. data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=2007 and month(order_date)=2; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; %let foot=No Internet Orders; end; else do; %let foot=Some Internet Orders; run; DATA step compilation is momentarily interrupted. ...

The DATA Step Interface Symbol Table month 2 year 2007 foot No Internet Orders The DATA Step Interface The macro variable FOOT is assigned. data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=2007 and month(order_date)=2; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; end; else do; %let foot=Some Internet Orders; run; Notice that, after the %let statement is acted on by macro processor, the macro processor "throws it in the trash can". It's gone. It is certainly not sent to the DATA step compiler, which is a good thing, because the DATA step compiler would have no idea what to do with it. ...

The DATA Step Interface Symbol Table The DATA Step Interface month 2 year 2007 foot Some Internet Orders The macro trigger overwrites the previous value. data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=2007 and month(order_date)=2; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; end; else do; %let foot=Some Internet Orders; run; month 2 year 2007 foot Some Internet Orders ...

The DATA Step Interface The compile phase is complete. Ready for execution. data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=2007 and month(order_date)=2; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; end; else do; run; month 2 year 2007 foot Some Internet Orders Nothing in this DATA step affects the value of FOOT. It remains Some Internet Orders. Similarly, the 2nd %let statement, after the macro processor acts on it, is "thrown in the trash can". The DATA step above is what was sent to, and seen by, the DATA step compiler. All along, the DATA step was compiling. The DATA executes only when the run statement is encountered.

The SYMPUTX Routine The SYMPUTX routine assigns to a macro variable any value available to the DATA step during execution time. It can create macro variables with: static values dynamic (data dependent) values dynamic (data dependent) names Symbol Table The SYMPUTX routine is a true DATA step statement. DATA step variables DATA step expressions character literals SYMPUTX

The SYMPUTX Routine The SYMPUTX routine is an executable DATA step statement. macro-variable is assigned the character value of text. If macro-variable already exists, its value is replaced. Literal values in either argument must be enclosed in quotation marks. CALL SYMPUTX(macro-variable, text); Argument 1 can be any character literal, variable, or expression that resolves to a legal macro variable name. Argument 2 can be any character or numeric constant, variable, or expression. Character literals are quoted.

The SYMPUTX Routine No macro triggers within DO groups %let month=2; %let year=2007; data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=&year and month(order_date)=&month; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; call symputx('foot', 'No Internet Orders'); end; else do; call symputx('foot', 'Some Internet Orders'); run; In this case, both arguments to the SYMPUTX routine are character literals. Therefore, both arguments are quoted. Fixed macro variable name Fixed macro variable value

%let month=2; %let year=2007; data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=&year and month(order_date)=&month; if order_type=3 then Number+1; if final then do; put Number=; if Number=0 then do; call symputx('foot', 'No Internet Orders'); end; else do; call symputx('foot', 'Some Internet Orders'); run; proc print data=orders; title "Orders for &month-&year"; footnote "&foot";

The SYMPUTX Routine You can copy the current value of a DATA step variable into a macro variable by using the name of a DATA step variable as the second argument to the SYMPUTX routine. A maximum of 32,767 characters can be assigned to the receiving macro variable. Values of numeric variables are automatically converted to character using the BEST12. format. Leading and trailing blanks are automatically removed from both arguments. CALL SYMPUTX('macro-variable', DATA-step-variable);

The SYMPUTX Routine %let month=1; %let year=2007; data orders; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=&year and month(order_date)=&month; if order_type=3 then Number+1; if final then call symputx('num', Number); run; proc print data=orders; title "Orders for &month-&year"; footnote "&num Internet Orders"; Since argument 2 is a numeric variable, the SYMPUTX routine converts it to character, as stated on the previous slide. No conversion note is written to the log.

Further enhance the footnotes. The dataset is sorted by Order_Date.

The SYMPUTX Routine You can use DATA step functions and expressions in the SYMPUTX routine's second argument to: format data values perform arithmetic operations on numeric data manipulate character data CALL SYMPUTX('macro-variable',expression);

%let month=1; %let year=2007; data orders (drop=last_order); retain last_order 0; keep order_date order_type quantity total_retail_price; set orion.order_fact end=final; where year(order_date)=&year and month(order_date)=&month; if order_type=3 then do; Number+1; sumprice+total_retail_price; if order_date>last_order then last_order=order_date; end; if final then do; call symputx('num', Number); call symputx('avg',put(sumprice/number,dollar8.)); call symputx("last",put(last_order,mmddyy8.)); run; proc print data=orders; title "Orders for &month-&year"; footnote "&num Internet Orders"; footnote2 "Average Internet Order: &avg"; footnote3 "Last Internet Order: &last"; title;footnote;

Passing Values between Steps Example: Based on user-selected time periods, dynamically compute statistics and include in titles. Previous examples stored the result of DATA step calculations into macro variables. What if you wanted to store the result of PROC step calculations into macro variables? The SYMPUTX routine is, of course, not valid in a proc step.

%let start=01Jan2007; %let stop=31Dec2007; proc means data=orion.order_fact noprint; where order_date between "&start"d and "&stop"d; var total_retail_price; output out=stats n=count mean=avg; run; data _null_; set stats; call symputx('num_orders',count); call symputx('avg_order',put(avg,dollar8.2)); %put Number of orders: &num_orders; %put Average price of orders: &avg_order;

Passing Values between Steps %let start=01Jan2007; %let stop=31Dec2007; proc means data=orion.order_fact noprint; where order_date between "&start"d and "&stop"d; var total_retail_price; output out=stats n=count mean=avg; run; data _null_; set stats; call symputx('Num_orders',count); call symputx('Avg_order',avg); %put Number of orders: &num_orders; %put Average order price: &avg_order; Use a 3-step approach. 1) Output proc step statistics to a dataset. 2) Read that dataset in a DATA _NULL_ step and create macro variables, 3) which can be referenced in the next step. The proc means step and the DATA step use the same input dataset. Ordinarily, we would apply the desired format, such as dollar4., to a macro variable's value using the PUT function in the SYMPUTX routine's 2nd argument. However, in this case, the REF= option on the VBAR3D statement requires a standard numeric value without dollar signs or commas. In this case, the dollar format is applied with %sysfunc in the footnote statement. PUTN is used because PUT is not available to %sysfunc.

title "All orders between &start and &stop"; title2 "Number of orders: &num_orders"; title3 "Average order: &avg_order"; proc print data=orion.order_fact ; where order_date between "&start"d and "&stop"d; var customer_id order_date order_id total_retail_price; run; title;