© OCS Biometric Support 1 APPEND, EXECUTE and MACRO Jim Groeneveld, OCS Biometric Support, ‘s Hertogenbosch, Netherlands. PhUSE 2010 – CC05 PhUSE 2010.

Slides:



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

Find It Using SAS By David Steves. Would you like to be able to search SAS programs for certain expressions? Example: I have a list of SAS programs which.
How SAS implements structured programming constructs
Examples from SAS Functions by Example Ron Cody
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Outline Proc Report Tricks Kelley Weston. Outline Examples 1.Text that spans columnsText that spans columns 2.Patient-level detail in the titlesPatient-level.
Macro Processor.
© OCS Biometric Support 1 MedDRA data as SAS formats Jim Groeneveld, OCS Biometric Support, Leiden, the Netherlands. SGF 170 –2009 SAS Global Forum 2009.
Statistics in Science  Introducing SAS ® software Acknowlegements to David Williams Caroline Brophy.
Basic And Advanced SAS Programming
1 Computer Applications in Epidemiology Dongmei Li Lecture 26 5/6/2009.
© OCS Biometric Support 1 Updating an MS SQL database from SAS Jim Groeneveld, OCS Biometric Support, ‘s Hertogenbosch, Netherlands. PhUSE 2010 – CC04.
Jeremy W. Poling B&W Y-12 L.L.C. Can’t Decide Whether to Use a DATA Step or PROC SQL? You Can Have It Both Ways with the SQL Function!
Using Proc Datasets for Efficiency Originally presented as a Coder’s NESUG2000 by Ken Friedman Reviewed by Karol Katz.
Let SAS Do the Coding for You! Robert Williams Business Info Analyst Sr. WellPoint Inc.
Automating survey data validation using SAS macros Eric Bush, DVM, MS Centers for Epidemiology and Animal Health Fort Collins, CO.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Creating SAS® Data Sets
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
FORMAT FESTIVAL AN INTRODUCTION TO SAS® FORMATS AND INFORMATS By David Maddox.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
© OCS Consulting The flexible extension to your IT team 1 Jim Groeneveld, OCS Consulting, ´s Hertogenbosch, Netherlands. PhUSE 2011 Comparing dataset metadata.
Different Decimal Places For Different Laboratory Tests PharmaSug 2004, TT01 A. Cecilia Mauldin.
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
Learning to love the SAS LAG function Phuse 9-12 October 2011 Herman Ament, MSD, Oss NL Phuse 9-12 October 2011.
Multiple Uses for a Simple SQL Procedure Rebecca Larsen University of South Florida.
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.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
1 PhUSE 2011 Missing Values in SAS Magnus Mengelbier Director.
EPIB 698C Lecture 2 Notes Instructor: Raul Cruz 2/14/11 1.
Lesson 2 Topic - Reading in data Chapter 2 (Little SAS Book)
Introduction to Using the Data Step Hash Object with Large Data Sets Richard Allen Peak Stat.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
1 Efficient SAS Coding with Proc SQL When Proc SQL is Easier than Traditional SAS Approaches Mike Atkinson, May 4, 2005.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
Jessica Bennett, Advance America Barbara Ross, Flexshopper LLC PharmaSUG 2015 Paper #QT06.
© OCS Biometric Support 1 SAS macro %_COUNT_ Jim Groeneveld, OCS Biometric Support, Leiden, the Netherlands. CC01 – PhUSE 2008.
Macro Variable Resolution Enio Presutto York University, Toronto, Canada.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Chapter 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Creating and Using Custom Formats for Data Manipulation and Summarization Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 17: Formatting Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1.
Writing and Reading XML files with SAS (Statistical Analysis System) What is SAS ? SAS Institute (or SAS, pronounced "sass") is an American developer of.
YET ANOTHER TIPS, TRICKS, TRAPS, TECHNIQUES PRESENTATION: A Random Selection of What I Learned From 15+ Years of SAS Programming John Pirnat Kaiser Permanente.
Copyright © 2004, SAS Institute Inc. All rights reserved. SASHELP Datasets A real life example Barb Crowther SAS Consultant October 22, 2004.
“LAG with a WHERE” and other DATA Step Stories Neil Howard A.
CC07 PhUSE 2011 Seven Sharp tips for Clinical Programmers David Garbutt Rohit Banga BIOP AG.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Copyright 2009 The Little Engine That Could: Using EXCEL LIBNAME Engine Options to Enhance Data Transfers between SAS® and Microsoft® Excel Files William.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Working Efficiently with Large SAS® Datasets Vishal Jain Senior Programmer.
Using the Macro Facility to Create HTML, XML and RTF output Rick Langston, SAS Institute Inc.
Better Metadata Through SAS® II: %SYSFUNC, PROC DATASETS, and Dictionary Tables.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 16 & 17 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
Hints and Tips SAUSAG Q SORTING – NOUNIQUEKEY The NOUNIQUEKEY option on PROC SORT is a useful way in 9.3 to easily retain only those records with.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
Chapter 2: Getting Data into SAS
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Macro Variable’s scope
Retrieving Macro Variables in the DATA Step
Combining Data Sets in the DATA step.
Lab 3 and HRP259 Lab and Combining (with SQL)
Presentation transcript:

© OCS Biometric Support 1 APPEND, EXECUTE and MACRO Jim Groeneveld, OCS Biometric Support, ‘s Hertogenbosch, Netherlands. PhUSE 2010 – CC05 PhUSE 2010

© OCS Biometric Support 2 APPEND, EXECUTE and MACRO AGENDA / CONTENTS 1.Drawbacks of PROC APPEND 2.Consequences of drawbacks 3.Alternatives for PROC APPEND 4.SAS macro %_Append_ 5.Application of %_Append_ 6.Drawbacks of CALL EXECUTE 7.%_Append_ and CALL EXECUTE 8.Macro calls from CALL EXECUTE

© OCS Biometric Support 3 APPEND, EXECUTE and MACRO What does PROC APPEND? 1.Concatenate two datasets that have the same structure: variables, their types, character lengths 2.With the FORCE option datasets may have different structures, PDV of 1 st dataset retained, consequences: a)extraneous lengths in 2 nd dataset discarded b)additional variables in 2 nd dataset discarded c)variable attributes in 2 nd dataset discarded, only keeping those of the 1 st dataset

© OCS Biometric Support 4 What does PROC APPEND? Three example datasets to be concatenated DATA One; LENGTH One $6; One = 'abcdef'; Format One $6.; INFORMAT One $CHAR6.; LABEL One='This is One'; RUN; DATA Two; LENGTH One Two $8; One = ' '; Two = ' '; RUN; DATA Three; LENGTH One $4 Two $12; Two = 'ABCDEFGHIJKL'; Three = 3; FORMAT One 10.; LABEL One='New label One'; FORMAT Two $CHAR12.; INFORMAT Two $12.; LABEL Three='This is Three'; RUN;

© OCS Biometric Support 5 What does PROC APPEND? Result of (twice) PROC APPEND DATA Appended; SET One; RUN; PROC APPEND BASE=Appended DATA=Two FORCE; RUN; PROC APPEND BASE=Appended DATA=Three FORCE; RUN; TITLE "twice PROC APPEND"; PROC PRINT DATA=Appended; RUN; The PROC PRINT result is: Obs One 1 abcdef {missing value} The PROC CONTENTS result is: Obs NAME TYPE LENGTH VARNUM LABEL FORMAT FORMATL FORMATD INFORMAT INFORML INFORMD 1 One This is One $ 6 0 $CHAR 6 0

© OCS Biometric Support 6 What does PROC APPEND? Consequences of drawbacks: 1.Not generally useful unless datasets have identical structures 2.Yet often used by programmers failing to know about these restrictions 3.This may lead to unintended, incorrect results (lacking variables, truncated character values)

© OCS Biometric Support 7 Consequences of drawbacks Alternatives for PROC APPEND 1.SAS data step, PDV retention: a.new variables in second dataset retained, but: b.variable attributes from first dataset where occurring retained, so extended character lengths in second dataset may be truncated DATA Appended; SET One Two Three; RUN; The PROC CONTENTS result is: The PROC PRINT result is: Obs One Two Three 1 abcdef ABCDEFGH 3 Obs NAME TYPE LENGTH VARNUM LABEL FORMAT FORMATL FORMATD INFORMAT INFORML INFORMD 1 One This is One $ 6 0 $CHAR Three This is Three Two $CHAR 12 0 $ 12 0

© OCS Biometric Support 8 Consequences of drawbacks Alternatives for PROC APPEND 2.PROC SQL (OUTER UNION CORR): a.new variables in second dataset retained, OK; b.variable labels from first dataset where occurring retained, OK; c.Character variable lengths maximised: OK! d.(in)formats retained from only first dataset, other (longer) formats in second dataset are discarded, NOK; e.new variables in second dataset thus have no associated (in) formats, NOK.

© OCS Biometric Support 9 Consequences of drawbacks Alternatives for PROC APPEND 2.PROC SQL example: PROC SQL; CREATE TABLE OneTwo AS SELECT * FROM One OUTER UNION CORR SELECT * From Two; CREATE TABLE OneTwo3 AS SELECT * FROM OneTwo OUTER UNION CORR SELECT * FROM Three; QUIT; PROC PRINT DATA=OneTwo3; RUN; The PROC PRINT result is: Obs One Two Three 1 abcdef ABCDEFGHIJKL 3 The PROC CONTENTS result is: Obs NAME TYPE LENGTH VARNUM LABEL FORMAT FORMATL FORMATD INFORMAT INFORML INFORMD 1 One This is One $ 6 0 $CHAR Three This is Three Two

© OCS Biometric Support Alternatives for PROC APPEND SAS macro %_Append_ 1.Variables from both datasets retained; 2.Character variable lengths maximised; 3.Character (in)formats from maximum occurring length (not from existing (in)formats, those are discarded); 4.Numerical (in)formats from: 1.firstly occurring named (in)format or; 2.maximum unnamed width and decimals. 5.Variable labels from first occurrence. 10

© OCS Biometric Support 11 SAS macro %_Append_ The PROC CONTENTS result is: Obs NAME TYPE LENGTH VARNUM LABEL FORMAT FORMATL FORMATD INFORMAT INFORML INFORMD 1 ONE This is One $ 8 0 $CHAR THREE This is Three TWO $ 12 0 $ 12 0 Remarks Character lengths determined from stored (reserved) length, not from maximum occupied length Neither one of the discussed methods appends datasets with same named variables of different types

© OCS Biometric Support 12 SAS macro %_Append_ Application of macro %_Append_ 1.Usual code to append datasets: %_Append_ (Base=Base_dataset, Data=Data_dataset) 2.From CALL EXECUTE in a data step: appending 3 datasets to Appended DATA Reference; INPUT Dataset $16.; CARDS; dataset1 dataset2 dataset3 ; RUN; DATA _NULL_; SET Reference; CALL EXECUTE ('%_Append_ (Base = Appended, Data = ' || TRIM(Dataset) || ');‘ ); RUN;

© OCS Biometric Support 13 Application of macro %_Append_ Macro %_Append_ from CALL EXECUTE 1.Macro call %_Append_ within single quotes to delay its resolution; 2.Yet, once such a macro runs all macro code is executed immediately while the embedded SAS code still is delayed. This is no problem unless: 3.Some macro code was intended to be dependent on SAS code results (after RUN; or QUIT;), then erroneous! This is a drawback of CALL EXECUTE.

© OCS Biometric Support 14 Drawback of CALL EXECUTE Example macro: %MACRO TargetMacro; DATA _NULL_; PUT '==1=='; RUN; %PUT ==2==; DATA _NULL_; PUT '==3=='; RUN; %PUT ==4==; %MEND TargetMacro; printing numbers 1 to 4 from data step and macro code. Calling macro: %PUT Directly called; %TargetMacro prints in order 1 – 2 – 3 – 4. %PUT Via CALL EXECUTE; DATA _NULL_; CALL EXECUTE ('%TargetMacro'); RUN; prints in order 2 – 4 – 1 – 3.

© OCS Biometric Support 15 %_Append_ from CALL EXECUTE Rewritten macro %_Append_ 1.All conditional code is SAS code; 2.All macro code meant to be processed in the beginning, controlling SAS code, not the other way around; 3.SAS code may generate macro values using CALL SYMPUT, that can be processed conditionally.

© OCS Biometric Support 16 Rewriting macros, CALL EXECUTE Calling macros from CALL EXECUTE 1.avoiding conditional macro code, like rewritten macro %_Append_; 2.without caring for conditional macro code, delaying all macro code until after the data step ends, like the SAS code, using the %NRSTR macro function.

© OCS Biometric Support 17 Calling macros from CALL EXECUTE Delaying macros from CALL EXECUTE General way to delay macro code such that the order of numbers output in the example is : %PUT Via CALL EXECUTE and %NRSTR(%)NRSTR; DATA _NULL_; * Delaying resolution as well; CALL EXECUTE('%NRSTR(%TargetMacro)'); RUN; Yet the whole call within single quotes! %_Append_ needed no revision after all.

© OCS Biometric Support 18 APPEND, EXECUTE and MACRO QUESTIONS & ANSWERS