Computing for Research I Spring 2014 January 22, 2014.

Slides:



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

Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
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.
The Assembly Language Level
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Macro Processor.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Computing for Research I Spring 2011 January 19, 2011
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
VBA Modules, Functions, Variables, and Constants
Week 5 IBS 520. ColdFusion Variables CF uses variables to store data in memory. There are many different types of variables; each has its own.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Guide To UNIX Using Linux Third Edition
Basic And Advanced SAS Programming
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
 Avoid repetitious SAS code  Create generalizable and flexible SAS code  Pass information from one part of a SAS job to another  Conditionally execute.
“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)
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.
Introduction to Shell Script Programming
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
1 Chapter 1: Introduction 1.1 Course Logistics 1.2 Purpose of the Macro Facility 1.3 Program Flow.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
©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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Visual Basic Programming
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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.
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.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
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.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 16 & 17 By Tasha Chapman, Oregon Health Authority.
Two “identical” programs
Intro to PHP & Variables
Conditional Processing
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 ©
Creating Macro Variables in the DATA Step
Macro Variable’s scope
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Defining and Calling a Macro
Global and Local Symbol Tables
3 Iterative Processing.
Presentation transcript:

Computing for Research I Spring 2014 January 22, 2014

 Avoid repetitious SAS code  Create generalizable and flexible SAS code  Pass information from one part of a SAS job to another  Conditionally execute data steps and PROCs  Dynamically create code at execution time

 Pros  Coding time – once familiar with macros  Updates – will propagate throughout code  Validation – only needs to be done once  Allows data-driven conditional processing  Cons  Initial coding is more complex  May take longer to compile or execute

 A tool for text substitution  Simple text strings or SAS language statements  A component of Base SAS  May be used with SAS procedures, graphics, data steps, ODS, etc.  Facility has two main components  Macro processor – portion of SAS that does the work  Macro language – syntax that communicates with the processor

Macro statements Macro processor Standard SAS statements

 Macro Variable  Syntax: &name  Refers to a macro variable name reference  Processor completes the text substitution  Macro  Syntax: %name  Refers to a macro call  Processor compiles the macro with text substitution

 Beginner Tips  Creating macro variables for text substitution  Dynamic macro variables using data  Defining and calling macros  Conditional processing using macros  Iterative processing using macros

 Read your LOG  Turn on system options to view text substitution and macro execution notes  Syntax: OPTIONS MPRINT SYMBOLGEN;  Default is NOMPRINT and NOSYMBOLGEN  Allows validation of code and aids in debugging  Focus on the program then incorporate the macros  Plan out your program before you start coding  Write syntax that works  Supplement the syntax with macros for efficiency

 SAS opening triggers several automatic macro variables  Global: these variables are available at any time in the session in any part of the code  User-defined variables may be added to global symbol table  Minimum length of 0 (missing)  Maximum length of 65,534 characters  Stores numeric values as character strings

SYSCMDLAST NON-SAS COMMAND ENTERED SYSDATECURRENT DATE IN DATE6. OR DATE7. FORMAT SYSDAY CURRENT DAY OF THE WEEK SYSDEVICCURRENT GRAPHICS DEVICE SYSDSNLAST SAS DATASET BUILT SYSINDEXNUMBER OF MACROS STARTED IN JOB SYSINFO SYSTEM INFORMATION GIVEN BY SOME PROCS SYSPRODINDICATES WHETHER A SAS PRODUCT IS LICENSED SYSSCPOPERATING SYSTEM WHERE SAS IS RUNNING SYSTIMESTARTING TIME OF JOB SYSVERSAS VERSION

 Display all automatic system macro variables after opening SAS  Syntax: %put _automatic_;  The following will appear in the SAS log after submitting the above code: AUTOMATIC SYSDATE 21JAN14 AUTOMATIC SYSDATE9 21JAN2014 AUTOMATIC SYSDAY Tuesday AUTOMATIC SYSTIME 17:45 AUTOMATIC SYSVER 9.2

 May want to include a run date on reports  “Report run as of January 21, 2014.”  Macro variables references begin with an ampersand (&) followed by a macro variable name  Syntax: &sysdate9  To create a footnote with a run date:  Syntax: footnote1 “Report run as of &sysdate9”;  Output: Report run as of 21JAN2014  Log: Macro variable SYSDATE9 resolves to 21JAN2014

 An efficient way of replacing text strings in SAS code.  Can be defined within a macro definition or within a statement that is outside a macro definition, referred to as OPEN code.  Are independent of SAS data set variables.

 Create your own macro variables  Helpful to include these at the top of the program or to create instructions at the top of the program of which variables will need updated  Global macro variable creation  %LET statement will create a macro variable  Syntax: %LET newvar=January 1, 2014;  The macro variable “newvar” now contains the text “January 1, 2014”  Syntax: title “Data as of &newvar”;  Output: Data as of January 1, 2014

 Naming Conventions  Must start with a letter or _  Can be followed by letters or digits  Cannot be a reserved word  /61885/HTML/default/viewer.htm#a htm  Cannot start with SYS, AF, or DMS  Defining the value of the macro variable  May be character or numeric  Do not use quotes around text

 Call the macro variable with the ampersand and the variable name  May be called from statements such as title or footnote, within data steps, and in procedures  Syntax: &newvar  Using quotes around a macro variable  Text statements such as title that call a macro variable must use double quotes (“)  The macro variable will not resolve if placed inside single quotes (‘)  Example

 Macro variables can also be used in both DATA steps and PROCs (procedures)  Can be used for numeric or character values  Only use quotes around the macro variable name if quotes would be used without macros  Always use double quotes  Example

 The ampersand & tells SAS that the text following it is a macro variable name  SAS considers all text following & to be part of the macro variable name until it encounters another & or other delimiter such as “ or ;  A period. can be used as a delimiter to separate a macro variable name from text  If a period is part of the text then include two periods

 Examples of the use of delimiters for macro variables in text  %let var1=final;  %let var2=exam; SyntaxResult Title “This is the &var1&var2”;This is the finalexam Title “This is the &var1..”;This is the final. Title “This is the &var1. &var2..”;This is the final exam.

 SAS frequently allows for more than one way to do things  The SYMPUT routine can also create macro variables  Can be used to create macro variables with static values or dynamic (data dependent) values  Creates a global variable if created in open code (outside a macro) similar to %let  Syntax: CALL SYMPUT(“macro variable name”,text);  If text appears in quotes then it is a literal value. If it appears without quotes as it does here, then it is a variable name.

 You create a report with multiple data listing tables. You receive a request to have the title of each table contain the number of observations.  Example: A listing of hundreds of Adverse Events  Write code to count the total observations in the table  Syntax: proc sql; create table total as select count(*) as freq from aetable; quit;  Create a macro variable that contains the total  Syntax: data _null_; set total; call symput(“tot”,freq); run;  Create a title statement that calls the macro variable  Syntax: title “This is the AE Listing (N=&tot.)”;  Output: This is the AE Listing (N=100)

 A macro enables you to write macro programs  Programs will enable you to do iterative and conditional processing  General syntax of a macro or macro definition:  %MACRO macro_name; macro_text %MEND macro_name;

 Macro name follows SAS variable naming conventions – identifies the macro  Macro text may include text, SAS statements, and macro variables, functions, or calls  After submitting a macro definition:  Macro language statements are checked for syntax errors and compiled  SAS statements are not checked for syntax errors

 Calling a macro causes it to execute  Call can be made anywhere in the program after the macro has been defined  Represents a macro trigger  Syntax for calling a macro:  % macro_name  Note that a semi-colon is not needed after the macro call

 Macros with keyword parameters  There are other types of parameters but these are the most easily identified in code  Syntax: %MACRO macro_name (keyword=value,…,keyword=value); macro text %MEND macro_name;  Keyword parameters  Can be specified in any order  Are assigned a default or null value after equal sign  Are local macro variables (as opposed to global)  They can only be called within the specified macro

 You need to be able to output the grade distribution by gender and type of sport for athletes at your college as quickly as possible.  Original Syntax: proc freq data=grades; where gender=“Male” and sport=“Football”; table grades; title “Grade Distribution for Male Football Players”; run;

 Define the macro syntax: %macro grade(gender=,sport=); proc freq data=grades; where gender=“&gender” and sport=“&sport”; table grades; title “Grade Distribution for &gender. &sport. Players”; run; %mend grade;  Call the macro: %grade(gender=Male,sport=Football)

 Create a customized graph of drug levels in the blood over time for several cohorts and dose levels, but not all  Original Syntax: title “Cohort 1, Dose Level 1 Drug Levels Over Time”; proc gplot data=new(where=(cohort=1 and dose=1)); plot dose_one_level*time; run; quit;  This syntax would need repeated for each cohort and dose level necessary

 Macro Syntax: %macro doses (cohort=,dose=,num=); title “Cohort &cohort., Dose Level &dose. Over Time”; proc gplot data=new(where=(cohort=&cohort. and dose=&dose.)); plot dose_&num._level*time; run; quit; %mend doses; %doses(cohort=1,dose=1,num=one) %doses(cohort=1,dose=2,num=two)

 Write and debug the SAS program without macro coding  Generalize by removing hardcoded constants and creating macro variables with %LET statements  Define a macro by placing %MACRO and %MEND statements around the program  Convert %LET statements to macro parameters

 Defining a macro allows you to use conditional processing outside of the data step  IF, THEN, ELSE, DO, END  Conditional keywords must have the % sign in front of them inside the macro definition  %IF, %THEN, %ELSE, %DO, %END  AND/OR are exceptions  This enables you to execute certain portions of the code based on the data

 You are creating a report with numerous tables by injury type. The reports for each injury type have identical information, however, the lab values are stored in different tables for each injury type. Run the report program to create a report by injury type using conditional processing.  Injury types: severe, mild  Respective lab table names: form01, form02

%MACRO report(injurytype=,num=); %IF &injurytype=severe %THEN %DO; data labs; set form01; run; %END; %ELSE %IF &injurytype=mild %THEN %DO; data labs; set form02; run; %END; proc means data=labs n mean std; var inr creatinine; run; quit; %MEND report; %report(injurytype=severe) %report(injurytype=mild)

 Iterative processing can be done within a macro definition  Can repeatedly execute macro statements  Can repeatedly generate SAS code  Keywords enable the processing  %DO, %END, %BY  The index variable (%I) is a macro variable  %START and %STOP macro variables are available

 Your client requests that a dataset be created for each subject. You have several hundred subjects in your study. How can you do this efficiently?  Use iterative processing and macro variables to create a loop that output the data for each subject into its own dataset

%macro subs ; proc sql; create table total as select count(distinct subject) as total_subjects from masterdata; quit; data _null_; set total; call symput ('total',total_subjects); run; %do i= 1 %to &total; data sub&i; set masterdata; if _n_=&i; run; %end; %mend subs; % subs

 Programming in SAS using macros can make your life easier  Reduces updates throughout programs  Reduces repetitious code  Allows for conditional and iterative processing outside the data step  Evaluate your programming goals to decide if macros are needed for each program  Start simple and expand to include all parts of macro programming