Macro Variable Resolution Enio Presutto York University, Toronto, Canada.

Slides:



Advertisements
Similar presentations
Chapter 3 – Fundamental Statements
Advertisements

Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Copyright © 2010 SAS Institute Inc. All rights reserved. SAS ® Macros: Top Ten Questions (and Answers!) Kevin Russell –Technical Support Analyst SAS Institute.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
VBA Modules, Functions, Variables, and Constants
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2: Introduction to C++.
Data Cleaning 101 Ron Cody, Ed.D Robert Wood Johnson Medical School Piscataway, NJ.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
I OWA S TATE U NIVERSITY Department of Animal Science Writing Flexible Codes with the SAS Macro Facility (Chapter in the 7 Little SAS Book) Animal Science.
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.
SCSUG Avoiding Hand Cramps: Name2 macro and arrays for related variables November 9, am South Center SAS Users Group Austin, Texas.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
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.
Managing Passwords in the SAS System Allen Malone Senior Analyst/Programmer Kaiser Permanente.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Web Design and Development for E-Business By Jensen J. Zhao Copyright 2003 Prentice Hall, Inc. Web Design and Development for E-Business Jensen J. Zhao.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Batch processing and sysparm A step towards scheduling.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
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 © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2004, SAS Institute Inc. All rights reserved. SASHELP Datasets A real life example Barb Crowther SAS Consultant October 22, 2004.
FP512 WEB PROGRAMMING 1 PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Do not put content on the brand signature area NOBS for Noobs David B. Horvath, CCP, MS PhilaSUG Winter 2015 Meeting NOBS for Noobs.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
SAS ® Global Forum 2014 March Washington, DC.
Using the Macro Facility to Create HTML, XML and RTF output Rick Langston, SAS Institute Inc.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Tips for Mastering Relational Databases Using SAS/ACCESS®
BASIC ELEMENTS OF A COMPUTER PROGRAM
Exploring JavaScript Ch 14
A First Book of ANSI C Fourth Edition
Two “identical” programs
Microsoft Access Illustrated
ISC440: Web Programming 2 Server-side Scripting PHP 3
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 ©
2.1 Parts of a C++ Program.
Creating Macro Variables and Assigning Value
Creating Macro Variables in the DATA Step
Macro Variable’s scope
PHP.
Defining and Calling a Macro
Global and Local Symbol Tables
Retrieving Macro Variables in the DATA Step
3 Iterative Processing.
Chapter 2: Introduction to C++.
How Best to Use Macro Quoting Functions?
PHP an introduction.
Tips and Tricks for Using Macros to Automate SAS Reporting.
Presentation transcript:

Macro Variable Resolution Enio Presutto York University, Toronto, Canada

Creating Macro Variables %let test=one; test is the name of the macro variable one is the value of the macro variable test value assigned can contain letters, numbers, printable characters and blanks

Simple Macro Resolution After a macro variable is created you can reference the variable as follows: &test %put &test; returns the value one

Assigning Text Constants %let addr=maple; %put &addr; returns value - maple leading and trailing blanks not stored quotation marks if included become part of the value

Assigning digits %let numa=123; %let numb= ; %put &numa; returns %put &numb; returns

Arithmetic Expressions %let numb=%eval( ); %put &numb; returns %let numb=%sysevalf( ); %put &numb; returns

Assigning a null value %let status=; %put &status; returns --

Assigning Macro Variable References %let comp=York University; %let addr=4700 keele st; %let who=&comp &addr, North York; %put &who; returns -- York University 4700 keele st, North York

Assigning special characters %let double=%str(one two); %put &double returns -- %let poss=%str(Enio%’s Place); &double returns -- one two &poss returns Enio’s Place

Scope Gobal - –exist for duration of SAS Session –can be referenced anywhere in the program, inside or outside macros Local –exist only during execution of the macro in which the variable is created

Direct Referencing %let dsn1=year1991; %let dsn2=year1992; %let dsn3=year1993; %let dsn4=year1994; %let dsn5=year1995;

Indirect Referencing %macro test; %do I=1 %to 5; %put &dsn&I; %put year199&I; %end; %test;

Indirect Referencing %macro test; %do I=1 %to 5; %put dsn&I =year199&I; %let dsn&I=year199&I; %end; %put &dsn1 &dsn2 &dsn3 &dsn4 &dsn5; %mend; %test; Returns dsn1 = year1991 dsn2 = year1992 dsn3 = year1993 dsn4 = year1994 dsn5 = year1991 year1992 year1993 year1994 year1995 year1995

SAS and the Web HTML form contains a pull down from which the user can select 1 or many options SAS/IntrNet broker sends this information via Macro Variables

Sample HTML year1991 year1992 year1993 year1994

Macro Variables Sent to Server If user select 1 option then server is sent the macro variable dsn and it will be assigned the value the user selects Symbols passed to SAS –#symbols: 2 ” –_debug" = "131" –“dsn” = “year1994”

Macro Variables Sent to Server If user select multiple options then server is sent the following: Symbols passed to SAS –#symbols: 2 ” –_debug" = "131" –“dsn” = “year1994” –“dsn0”= “2” –“dsn1”=“year1994” –“dsn2”=“year1995”

Determining what has been sent %let dsn0=2; %let dsn1=year1994; %let dsn2=year1995; %macro test; %do i=1 %to &dsn0; %put &&dsn&i; %end; %mend; %test; Returns year1994 year1995

Determining what has been sent %let dsn=year1994; %macro test; %do i=1 %to &dsn0; %put &&dsn&i; %end; %mend; %test; Returns WARNING: Apparent symbolic reference DSN0 not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &DSN0 ERROR: The %TO value of the %DO I loop is invalid. ERROR: The macro will stop executing.

Resolving the Problem %macro enio; proc sql ; create table vmac as select * from dictionary.macros; quit; run;

Results GLOBAL SQLRC 0 GLOBAL DSN year1995

Sample Program data _null_; length valuea $ 200; length fnd_dsn0 fnd_dsn1 $ 1; retain fnd_dsn0 fnd_dsn1 valuea; set vmac end=eof;

Sample Program if _n_ eq 1 then do; fnd_dsn0='N'; fnd_dsn1='N'; end;

Sample Program if name = upcase('dsn') then do; call symput("ndsn",left(value)); valuea=value; end;

Sample Program if name = upcase('dsn0') then do; fnd_dsn0='Y'; call symput("ndsn0",left(value)); end;

Sample Program if name = upcase('dsn1') then do; fnd_dsn1='Y'; call symput("ndsn1",left(value)); end;

Sample Program if name = upcase('dsn2') then call symput("ndsn2",left(value)); if name = upcase('dsn3') then call symput("ndsn3",left(value)); if name = upcase('dsn4') then call symput("ndsn4",left(value)); if name = upcase('dsn5') then call symput("ndsn5",left(value)); if name = upcase('dsn6') then call symput("ndsn6",left(value));

Sample Program if eof then do; if fnd_dsn0 eq 'N' and fnd_dsn1 eq 'N' then do; call symput("ndsn0",left('1')); call symput("ndsn1",left(trim(valuea))); end; end;3 run %mend;

Sample Program %let dsn=year1994; %macro test; %enio; %do i=1 %to &ndsn0; %put &&ndsn&i; %end; %mend; %test; run; Returns GLOBAL NDSN0 1 GLOBAL NDSN year1994 GLOBAL NDSN1 year1994

Sample Program %let dsn0=2; %let dsn1=year1994; %let dsn2=year1995;; %macro test; %enio; %do i=1 %to &ndsn0; %put &&ndsn&i; %end; %mend; %test; run; Returns GLOBAL NDSN0 2 GLOBAL NDSN year1994 GLOBAL NDSN1 year1994 GLOBAL NDSN2 year1995

Copyrights The SAS system and SAS/IntrNet are registered trademarks of The SAS Institute Inc, Cary North Carolina, U.S.A.