SCSUG Avoiding Hand Cramps: Name2 macro and arrays for related variables November 9, 2010 9am South Center SAS Users Group Austin, Texas.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
JavaScript, Third Edition
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Syntax MROUND(number,multiple) Number is the value to round. Multiple is the multiple to which you want to round number. Remark MROUND rounds up, away.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Lists in Python.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
JavaScript, Fourth Edition
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Topics Designing a Program Input, Processing, and Output
The Selection Structure
Type Conversion, Constants, and the String Object
Microsoft Access Illustrated
Macro Expressions There are three types of macro expressions: text, logical, and arithmetic. A text expression is any combination of text, macro variables,
Control Structures: if Conditional
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Control Structures: for & while Loops
Using Lists and Functions to Create Dialogue
How Best to Use Macro Quoting Functions?
Topics Designing a Program Input, Processing, and Output
The Data Element.
Topics Designing a Program Input, Processing, and Output
The Selection Structure
The Data Element.
DATA TYPES AND OPERATIONS
Class code for pythonroom.com cchsp2cs
ICS 101 Lab 3 Hossain Arif ICS Dept
REPETITION Why Repetition?
Presentation transcript:

SCSUG Avoiding Hand Cramps: Name2 macro and arrays for related variables November 9, am South Center SAS Users Group Austin, Texas

Problem: Report percentages and per animal-type on subtotal based on family subtotals and zoo census

Macro Code %macro name2(macrovar,suf); %let i=0; %do %while(%scan(&macrovar,&i+1,%str( )) ne %str( )); %let i = %eval(&i+1); %let var=%scan(&macrovar,&i,%str( )); &var.&suf %end; %mend name2; Source: Al McKenzie, Texas Education Agency

%do %while  can be any macro expression that resolves to a logical value.  The macro processor evaluates the expression at the top of each iteration.  The expression is true if it is an integer other than zero.  The expression is false if it has a value of zero.  If the expression resolves to a null value or to a value containing nonnumeric characters, the macro processor issues an error message. Source:

(%scan  The %SCAN and %Q SCAN functions search argument and return the nth word.  A word is one or more characters separated by one or more delimiters.  %SCAN does not mask special characters or mnemonic operators in its result, even when the argument was previously masked by a macro quoting function.  %QSCAN masks the following special characters and mnemonic operators in its result: & % ' " ( ) + - * / = ¬ ^ ~ ;, blank AND OR NOT EQ NE LE LT GE GT Source:

(&macrovar,&i+1,%str(  What is a next value from the parameter list? This increments that value and prepares the parameter for additional manipulation 1.The variable name is retried as the first word. 2.The %( is used to mark the open parenthesis as a word delimiter 3.Notice the use of %( and %) to designate the open and close parenthesis. 4.The list of parameters is temporarily stored in &macrovar 5.The resulting list of comparisons is passed back Source: Carpenter's Complete Guide to the SAS Macro Language, Second EditionCarpenter's Complete Guide to the SAS Macro Language, Second Edition

ne %str( ));  If a special character or mnemonic affects the way the macro processor constructs macro program statements, you must mask the item during macro compilation  (or during the compilation of a macro program statement in open code) by using either the %STR or %NRSTR macro quoting functions.  These macro quoting functions mask the following special characters and mnemonics: Blank)=NE ;(|LE ¬+#LT ^--ANDGE ~*ORGT, (comma)/NOT ‘ EQ Source:

%eval(&i+1);  The %EVAL function evaluates integer arithmetic or logical expressions. %EVAL operates by converting its argument from a character value to a numeric or logical expression. Then, it performs the evaluation.  Finally, %EVAL converts the result back to a character value and returns that value. Source: Art Carpenter, If all operands can be interpreted as integers, the expression is treated as arithmetic. If at least one operand cannot be interpreted as numeric, the expression is treated as logical. If a division operation results in a fraction, the fraction is truncated to an integer. So how does it process? 1.Checks to see if there is a next word. If there is then enter the loop so that &i can be incremented ï. 2.Since the &i+1 word was found, increment the value of &i. 3.When the last word has been counted, exit the loop and return the number of words counted by passing it back.

%let var=%scan(&macrovar,&i,%str( )); TThe VARNAME function returns the name of the &Ith variable. This variable is appended to the growing list of variables stored in &VARLIST. TThe %SCAN function can then be used to identify and breakout the individual array elements. tthe %SCAN function have two required and one optional argument and they take the form of: %scan(macro_var_list, word_number, word_delimiter) %%SCAN looks ahead to see if the next word (&COUNT + 1) exists. Source: Art Carpenter,

&var.&suf &&var NVAR variable from a given dataset which are contained in a call macro like %tokeep which contains the names of variables of the dataset and create macro variables. Then resolve these macro variables in a macro DO LOOP to perform any necessary function. &&suf add the suffix "_base" to all the variables in the sas dataset. Sources:

A second look %macro name2(macrovar,suf); %let i=0; %do %while(%scan(&macrovar,&i+1,%str( )) ne %str( )); %let i = %eval(&i+1); %let var=%scan(&macrovar,&i,%str( )); &var.&suf %end; %mend name2;  %let tokeep=dog wolf fox canine Cat lion tiger feline Pig swine boar porcine;

Problem: Report percentages and per animal-type on subtotal based on family subtotals and zoo census

Adding a subtotal to an all total * Keep in mind that food expense is a subset of all expenses, and other expense types need the addition of food; %let tokeep=DOG WOLF FOX CANINE CAT LION TIGER FELINE PIG SWINE BOAR PORCINE; array items {*} &tokeep ; array all {*} %name2(&tokeep,4); array food {*} %name2(&tokeep,1) ; do i = 1 to dim(items); all{i} = sum(0, food{i}, all{i}); end; EOBTOT1 = SUM(0, DOG, WOLF, FOX, CAT, LION, TIGER, PIG, SWINE, BOAR);

Treating for unique denominators We need to assign a total variable for food totals and another total variable for food and the sum of other totals. We use the same logic of giving food a suffix of 1 and food and all other expenses sum with a suffix of 4 These are represented as EOBTOT1 AND EOBTOT4 for all of the animals

Treating for a global zoo population; use several %tokeep,n, but have one master list %let tokeep1=CANINES DOG WOLF FOX; %let total1 =CANINES; %let tokeep2=FELINES CAT LION TIGER; %let total2 =FELINES; %let tokeep3=PORCINES PIG SWINE BOAR; %let total3 =PORCINES; DATA BUDGET(keep=year quarter zoo_pop %name2(&tokeep,1) %name2(&tokeep,2) %name2(&tokeep,3) %name2(&tokeep,4) %name2(&tokeep,5) %name2(&tokeep,6));

Calculating the sub-expense & total expense for groups, subgroups array allpop {*} %name2(&tokeep,6); array foodpop {*} %name2(&tokeep,3) ; array items {*} &tokeep ; array all {*} %name2(&tokeep,4); array gen {*} %name2(&tokeep,1) ; do i = 1 to dim(items); if zoo_pop ne 0 then foodpop{i} = ROUND( gen {i} / zoo_pop); if zoo_pop ne 0 then allpop{i} = ROUND( all {i} / zoo_pop); end;

Review: use several %tokeeps with several sub-populations to report %let tokeep1=CANINES DOG WOLF FOX; %let total1 =CANINES; %let tokeep2=FELINES CAT LION TIGER; %let total2 =FELINES; %let tokeep3=PORCINES PIG SWINE BOAR; %let total3 =PORCINES; Are different assignments from %let tokeep=DOG WOLF FOX CAT LION TIGER PIG SWINE BOAR;

%LET STATEMENT %let tokeep=DOG WOLF FOX CAT LION TIGER PIG SWINE BOAR; “%Tokeep” is effective if there is a treatment that requires the total population to apply as a denominator against the master array.

When Denominators differ for different sub-group calculations, count on the numerically suffixed %tokeeps %let tokeep1=DOG WOLF FOX ; %let total1 =CANINE; %let tokeep2=CAT LION TIGER; %let total2 =FELINE; %let tokeep3=PIG SWINE BOAR; %let total3 =PORCINE;

Assigned subtotals act as denominators: What a dog?! *DOG;array items1 {*} &tokeep1; array all1 {*} %name2(&tokeep1,4) %name2 (&total1,4 ); array allperc1 {*} %name2(&tokeep1,5) %name2 (&total3,5 ); array gen1 {*} %name2(&tokeep1,1) %name2 (&total1,1 ); array genperc1 {*} %name2(&tokeep1,2) %name2 (&total1,2 ); do i = 1 to dim(items1); if &total1.1 not in(0,.) then genperc1{i} = ROUND((gen1{i} / &total1.1 ) * 100,.01); if &total1.4 not in(0,.) then allperc1{i} = ROUND((all1{i} / &total1.4 ) * 100,.01); end;

Second look at denominators *DOG;do i = 1 to dim(items1); if &total1.1 not in(0,.) then genperc1{i} = ROUND((gen1{i} / &total1.1) * 100,.01); if &total1.4 not in(0,.) then allperc1{i} = ROUND((all1{i} / &total1.4) * 100,.01); end; *CAT; do i = 1 to dim(items2); if &total2.1 not in(0,.) then genperc2{i} = ROUND((gen2{i} / &total2.1) * 100,.01); if &total2.4 not in(0,.) then allperc2{i} = ROUND((all{i} / &total2.4) * 100,.01); end; *PIG;do i = 1 to dim(items6); if &total3.1 not in(0,.) then genperc3{i} = ROUND((gen3{i} / &total6.1) * 100,.01); if &total3.4 not in(0,.) then allperc3{i} = ROUND((all3{i} / &total3.4) * 100,.01); end;

Macro and array summary %macro name2(macrovar,suf); %let i=0; %do %while(%scan(&macrovar,&i+1,%str( )) ne %str( )); %let i = %eval(&i+1); %let var=%scan(&macrovar,&i,%str( )); &var.&suf %end; %mend name2;

ANY QUESTIONS ?????????????????????????? David B. Cohen, Systems Analyst School Finance Unit Website: School Finance Unit Phone Number: (512) Fax Number: (512)