Computing for Research I Spring 2011 January 19, 2011

Slides:



Advertisements
Similar presentations
Effecting Efficiency Effortlessly Daniel Carden, Quanticate.
Advertisements

VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
I OWA S TATE U NIVERSITY Department of Animal Science Using Basic Graphical and Statistical Procedures (Chapter in the 8 Little SAS Book) Animal Science.
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Entering Data for Analysis Annie Herbert Medical Statistician Research & Development Support Unit Salford Royal (Hope) Hospitals NHS Foundation Trust
Knowing Understanding the Basics Writing your own code part 2 SAS Lab.
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71.
1 SAS Formats and SAS Macro Language HRP223 – 2011 November 9 th, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
1 BA 275 Quantitative Business Methods Housekeeping Introduction to Statistics Elements of Statistical Analysis Concept of Statistical Analysis Exploring.
VBA Modules, Functions, Variables, and Constants
Introduction to SQL Session 1 Retrieving Data From a Single Table.
 Avoid repetitious SAS code  Create generalizable and flexible SAS code  Pass information from one part of a SAS job to another  Conditionally execute.
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.
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.
A Concise Display of Multiple Response Items Patrick Thornton.
SAS PROC REPORT PROC TABULATE
Chapter 9 Producing Descriptive Statistics PROC MEANS; Summarize descriptive statistics for continuous numeric variables. PROC FREQ; Summarize frequency.
HPR Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
Niraj J. Pandya, Element Technologies Inc., NJ.  Summarize all possible combinations of class level variables even if few categories are altogether missing.
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
Key Data Management Tasks in Stata
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
Adventures in ODS: Producing Customized Reports Using Output from Multiple SAS® Procedures Stuart Long Westat, Durham,
1 Experimental Statistics - week 2 Review: 2-sample t-tests paired t-tests Thursday: Meet in 15 Clements!! Bring Cody and Smith book.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
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.
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 A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Chapter 5 Reading and Manipulating SAS ® Data Sets and Creating Detailed Reports Xiaogang Su Department of Statistics University of Central Florida.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Lesson 8 - Topics Creating SAS datasets from procedures Using ODS and data steps to make reports Using PROC RANK Programs in course notes LSB 4:11;5:3.
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.
Time Series Data Processes by Tai Yu April 15, 2013.
An Introduction Katherine Nicholas & Liqiong Fan.
Efficient SAS programming with Large Data Aidan McDermott Computing Group, March 2007.
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.
Customize SAS Output Using ODS Joan Dong. The Output Delivery System (ODS) gives you greater flexibility in generating, storing, and reproducing SAS procedure.
1 PEER Session 02/04/15. 2  Multiple good data management software options exist – quantitative (e.g., SPSS), qualitative (e.g, atlas.ti), mixed (e.g.,
Second Period: PROC TABULATE Jill Casey. Example 1: Simple 2D Table.
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
Basics in R part 2. Variable types in R Common variable types: Numeric - numeric value: 3, 5.9, Logical - logical value: TRUE or FALSE (1 or 0)
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Descriptive Statistics using R. Summary Commands An essential starting point with any set of data is to get an overview of what you are dealing with You.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
SAS Certification Prep Guide Chapter 7 Creating and Applying User-Defined Formats.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
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.
Session 1 Retrieving Data From a Single Table
JavaScript Objects.
Two “identical” programs
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Lesson 8 - Topics Creating SAS datasets from procedures
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 ©
Macro Variable’s scope
Never Cut and Paste Again
Producing Descriptive Statistics
Functions continued.
Presentation transcript:

Computing for Research I Spring 2011 January 19, 2011 SAS: Macros Computing for Research I Spring 2011 January 19, 2011 

What are Macros and why do we need /use them? A macro is a way to automate a task that you perform repeatedly or on a regular basis. It is a series of commands and actions that can be stored and run whenever you need to perform the task. Can you think of situations where you may want or have used a macro?

SAS Procs In essence all of the PROC statements in SAS are macros. They have been written, validated and incorporated into the system so that they can be used repeatedly when called to perform specific tasks. Example: PROC means

PROC means Provides data summarization tools to compute descriptive statistics for variables across all observations and within groups of observations. Calculates descriptive statistics based on moments Estimates quantiles, including the median Calculates confidence limits for the mean Indentifies extreme values Performs a t test

What is the macro facility? A tool for extending and customizing SAS and for reducing the amount of text you must enter to do common tasks. Enables you to assign a name to character strings or groups of SAS programming statements.

Components of macro facility: The macro processor The portion of SAS that does the work The macro language The syntax that you use to communicate with the macro processor

How do we “trigger” the SAS macro processor? &name Refers to a macro variable The form &name is called a macro variable reference. %name Refers to a macro The form %name is called a macro call.

What we will cover: Macro Variables and Using them for specific functions SAS defined macro variables How do we use these in conjunction with SAS code? Creating SAS Code Using Macros Defining SAS macros SYNTEX for SAS macros Calling SAS macros Applications Examples

Macro Variables 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.

Macro Variables defined by SAS When you invoke SAS, the macro processor creates automatic macro variables that supply information related to the SAS session.

Macro Variables defined by SAS Some automatic SAS macro variables SYSCMD LAST NON-SAS COMMAND ENTERED SYSDATE CURRENT DATE IN DATE6. OR DATE7. FORMAT SYSDAY CURRENT DAY OF THE WEEK SYSDEVIC CURRENT GRAPHICS DEVICE SYSDSN LAST SAS DATASET BUILT SYSINDEX NUMBER OF MACROS STARTED IN JOB SYSINFO SYSTEM INFORMATION GIVEN BY SOME PROCS SYSPROD INDICATES WHETHER A SAS PRODUCT IS LICENSED SYSSCP OPERATING SYSTEM WHERE SAS IS RUNNING SYSTIME STARTING TIME OF JOB SYSVER SAS VERSION

Macro Variables defined by SAS To use an automatic macro variable, reference it with an ampersand followed by the macro variable name. Example: SYSDATE and SYSDAY SYSDATE contains a SAS date value in the DATE7. format, which displays a two-digit date, the first three letters of the month name, and a two-digit year. SYSDAY contains a SAS day value This FOOTNOTE statement contains references to the automatic macro variables SYSDAY and SYSDATE9: footnote "Report for &sysday, &sysdate9"; If the current SAS session is invoked on December 17, 2007, macro variable resolution causes SAS to receive this statement: FOOTNOTE "Report for Monday, 17DEC2007"; %put This SAS session started running on: &sysday, &sysdate.; Writes this to the SAS log: This SAS session started running on: Tuesday, 16DEC97

Macro Variables defined by Users Scope – Global vs Local %GLOBAL – used to define a global macro variable Naming conventions The scope determines how it is assigned values and how the macro processor resolves references to it. Two types of scopes: local and global. Global macro variables exist for the duration of the SAS session and can be references anywhere in the program, either inside or outside a macro. Local macro variables exist only during the execution of the macro in which the variables are created and have no meaning outside the defining macro. Macro variable names must start with a letter or an underscore and can be followed by letters or digits. You can assign any name to a macro variable as long as the name is not a reserved word. (See handout.)

Macro Variables defined by Users %LET How do we use this? %LET CITY=CHARLESTON; You can create your own macro variables, change their values, and define their scope. The simplest way to create and assign a value to a macro variable is to use the %let macro program statement. If we use the %LET in open code then this creates a global macro variable that is available for use anywhere in your SAS code during the SAS session in which the variable was created. If we use the %LET within the macro definition then it is considered a local macro variable within the macro definition where it was created. Now you can use the macro variable CITY in SAS statements where you’d like the text Charleston to appear. You refer to the variable by preceding the variable name with an ampersand (&). For example you could use the title statement: TITLE “Demographics Data for &CITY”; The macro processor resolves the references to the macro variable CITY, and the statement becomes TITLE “Demographics Data for Charleston”; Also note that the title statement uses double quotes. This is important to note because in open code, the macro processor resolves macro variable references within double quotation marks but not within single quotation marks. Generally, the macro processor treatment alphabetic characters, digits, and symbols (except & and %) as characters. It does not make a distinction between character and numeric values as the rest of SAS does.

Macro Program Statements %DO %GLOBAL %LOCAL %MACRO and %MEND In addition to the %LET statement, some of the other features of the macro language that create macro variables are listed here. We talked a little about %GLOBAL earlier. It can be used to declare a macro variable as global in scope in the same way that %LOCAL can be used to define a local macro variable. %DO may be used in iterative loops. %MACRO and %MEND are used when creating your own macros to begin and end a macro definition. We will look at these later. But I should mention that these are but a few examples of macro program statements. There are others such as %WINDOW (defines customized windows) and %DISPLAY (displays a macro window) which can be used in designing customized output for reports, as well as %PUT (writes text or the values of macro variables to the SAS log), %INPUT (supplies values to macro variables during macro execution), etc.

Macro Functions Processes one or more arguments and produces a result. Can use all macro functions in both macro definitions and open code. Examples: (%EVAL, %LENGTH, %UPCASE) %EVAL - evaluates arithmetic and logical expressions using integer arithmetic. %LENGTH - eturns the length of a string %UPCASE - convert characters to uppercase

Using and Displaying Macro Variables & statement %PUT Example: %LET A=2; %LET B=5; %LET OPERATOR=+; %PUT THE RESULT OF &A &OPERATOR &B IS %EVAL(&A &OPERATOR &B).; Like in the example looking at %LET, after a macro variable is created, you typically use the variable by referencing it with an ampersand presceding its name (&variable-name), which is referred to as a macro variable reference. These references can be used anywhere in a SAS program. The simplest way to display macro variable values is to use the %PUT statement, which writes text to the SAS log. When you submit these statements, the %PUT statement will write the following line to the SAS log: The result of 2 + 5 is 7.

Notes to consider about macro variables: Double quotation marks, i.e. “ “ vs ‘ ‘ Variable length Special characters Condition operations Complex tasks Macro variables are not subject to the same length limits as SAS data set variables. If the value you want to assign to a macro variable contains certain special characters (i.e. semicolons, quotation marks, ampersands, and percent signs) or mneumonics (i.e. AND, OR, or LT) then you must use a macro quoting function to mask the special characters. Otherwise, the special character or mneumonic might be misinterpreted by the macro processor. Macro variables cannot perform conditional operations, DO loops, and other complex tasks. For this kind of work you must define a macro.

Generating SAS Code Using Macros Macro Processing Need to define what the macro function will be Process that SAS uses to program macro elements

Defining and Calling Macros %MACRO macro-name; macro text %MEND macro-name; To start with you need to create a macro definition. The general form of a macro definition starts with the %MACRO statement and the name of the macro that you are creating. This is followed by the macro text which defines the function that this macro will perform using SAS statements. Then it ends with the %MEND statement. The macro name is a unique SAS name that identifies the macro. The macro text is any combination of macro statements, macro calls, text expressions, or constant text.

Application in SAS code: Vitals data over time Summary statistics using PROC TABULATE Categorical variables Continuous variables

Vitals Data over time: DATA FORM05; SET ALISAH.FORM05; SUBJECTID=ZSUBJECTID; MAX_CVPRESS=F05Q02; WEIGHT=F05Q06; MAX_SBP=F05Q07; MAX_DBP=F05Q09; MIN_DBP=F05Q10; MAX_HR=F05Q13; KEEP SUBJECTID ZVISITID MAX_CVPRESS WEIGHT MAX_SBP MAX_ MAX_HR; RUN; PROC SORT DATA=FORM05 OUT=FORM05T;BY SUBJECTID ZVISITID; Suppose you are conducting a clinical trial where vitals are captured over time and this is something that you are interested in displaying for reporting purposes. Perhaps the DSMB is looking at how vitals my shift over time during the treatment phase. The data are collected according to the following Schedule of Assessments (see Schedule of Assessments handout). But when I get the dataset, it contains multiple records per subject with a visit indicator (1-16) to represent each of the visits from baseline to day 15. I may also be interested in change from baseline for each of these vitals so I need to have the data in “flat” form (one record per subject) rather than “stacked” form so that I can do this manipulation and display the data as I am required. So let’s look at the case report form that contains the data that we collected on vitals. For simplicity we will only focus on 5 of the variables presented here: maximum CVP, weight, max SBP, max DBP, and max HR.

Vitals Data over time: *MACRO TO CREATE MULTIPLE DATA SETS-ONE FOR EACH DAY OF VITALS; %MACRO VITALS(DAY, VISIT); DATA &DAY._VITAL (KEEP=SUBJECTID MAX_CVPRESS& WEIGHT&DAY MAX_SBP&DAY MAX_DBP&DAY MIN_DBP&DAY MAX_HR&DAY); SET FORM05T; IF ZVISITID=&VISIT; MAX_CVPRESS&DAY=MAX_CVPRESS; WEIGHT&DAY=WEIGHT; MAX_SBP&DAY=MAX_SBP; MAX_DBP&DAY=MAX_DBP; MAX_HR&DAY=MAX_HR;

Vitals Data over time: LABEL MAX_CVPRESS&DAY="MAXIMUM CENTRAL VENOUS PRESSURE&DAY."; LABEL WEIGHT&DAY="WEIGHT&DAY."; LABEL MAX_SBP&DAY="MAX SYSTOLIC BLOOD PRESSURE&DAY."; LABEL MAX_DBP&DAY="MAX DIASTOLIC BLOOD PRESSURE&DAY."; LABEL MAX_HR&DAY="MAX HEART RATE&DAY."; RUN; PROC SORT DATA=&DAY._VITAL; BY SUBJECTID; RUN; %MEND VITALS;

Vitals Data over time: *CALLING MACRO TO CREATE THE DATA SETS FOR VITALS; %VITALS (BASE, 1); %VITALS (D1, 2); %VITALS (D2, 3); %VITALS (D3, 4); %VITALS (D4, 5); %VITALS (D5, 6); %VITALS (D6, 7); %VITALS (D7, 8);

Vitals Data over time: /*MACRO TO CREATE NEW VARIABLES TO BE USED IN MERED DATA SET TO CALCULATE DIFFERENCE EACH DAY FROM BASELINE*/ %MACRO CHANGES(NEWVAR, VAR1, VAR2, VITAL, DAY); IF &VAR1 NE . AND &VAR2 NE . THEN %DO; &NEWVAR=&VAR1-&VAR2; %END; LABEL &NEWVAR="CHANGE IN &VITAL. FROM BASELINE TO &DAY."; %MEND CHANGES;

PROC TABULATE for Categorical Variables: *CATEGORICAL MACRO BY TREATMENT GROUP; %MACRO CATEGORY( INDATA, MYVAR, MYTITLE) ; DATA INDATA;SET &INDATA; RUN; PROC TABULATE DATA=INDATA MISSING FORMAT=7.0 STYLE=[FONT_SIZE=1.5]; CLASS &MYVAR ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE] ; TABLE ALL="ALL SUBJECTS" &MYVAR=" ", (ZTREATMENTCODE ALL="ALL GROUPS")*(N PCTN<&myvar ALL>*F=PCTFMT7.) /BOX=[LABEL="&MYTITLE" STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]] MISSTEXT="0" PRINTMISS; LABEL ZTREATMENTCODE="TREATMENT GROUP"; KEYLABEL N="N" PCTN="PERCENT"; CLASSLEV &MYVAR ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; KEYWORD ALL N PCTN /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; RUN; %MEND; Sample code that will produce proc tabulate tables for a categorical variable by treatment group and display the column percent or percent within a treatment group for displays.

TREATMENT GROUP ALL GROUPS TIER 1 (0.625 G/KG) TIER 2 (1.25 G/KG) TIER 3 (1.875 G/KG) N PERCE NT GENDER ALL SUBJECTS 20 100.0% 7 47 MALE 5 25.0% 35.0% 1 14.3% 13 27.7% FEMALE 15 75.0% 65.0% 6 85.7% 34 72.3%

PROC TABULATE for Categorical Variables: %MACRO CONTINUOUS (INDATA, MYVAR, MYTITLE); PROC TABULATE DATA=&INDATA FORMAT=10.0 STYLE=[FONT_SIZE=1.5]; VAR &MYVAR/STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; CLASS ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; TABLE ALL="ALL RANDOMIZED" &MYVAR*(N="TOTAL_N" MEAN*F=6.2 STD="SD"*F=4.2 MEDIAN MIN MAX), (ZTREATMENTCODE ALL="ALL GROUPS") /BOX=[LABEL="&MYTITLE" STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]]; KEYLABEL N=" "; CLASSLEV ZTREATMENTCODE /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; KEYWORD ALL N MEAN STD MEDIAN MIN MAX /STYLE=[FONT_SIZE=1.5 FOREGROUND=BLACK BACKGROUND=WHITE]; RUN; %MEND; Sample code that will produce proc tabulate tables for a continuous variable by treatment group and displays the summary statistics within a treatment group for displays.

TREATMENT GROUP ALL GROUPS TIER 1 (0.625 G/KG) TIER 2 (1.25 G/KG) TIER 3 (1.875 G/KG) AGE AT BASELINE TOTAL N 20 7 47 Mean 50.80 51.45 53.57 51.49 SD 15.2 11.4 13.0 13.1 Median 51 55 Min 25 33 38 Max 79 77 75