Submitting SAS Code On The Side

Similar documents
SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

Functions vs. Macros: A Comparison and Summary

Tired of CALL EXECUTE? Try DOSUBL

The new SAS 9.2 FCMP Procedure, what functions are in your future? John H. Adams, Boehringer Ingelheim Pharmaceutical, Inc.

SAS Macro Dynamics: from Simple Basics to Powerful Invocations Rick Andrews, Office of Research, Development, and Information, Baltimore, MD

Using PROC FCMP to the Fullest: Getting Started and Doing More

RUN_MACRO Run! With PROC FCMP and the RUN_MACRO Function from SAS 9.2, Your SAS Programs Are All Grown Up

Journey to the center of the earth Deep understanding of SAS language processing mechanism Di Chen, SAS Beijing R&D, Beijing, China

Introduction. Getting Started with the Macro Facility CHAPTER 1

PharmaSUG 2017 Paper BB02

User-Written DATA Step Functions Jason Secosky, SAS Institute Inc., Cary, NC

Using PROC FCMP to the Fullest: Getting Started and Doing More

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

The SAS Interface to REXX

Your Own SAS Macros Are as Powerful as You Are Ingenious

New Macro Features Added in SAS 9.3 and SAS 9.4

Storing and Reusing Macros

Foundations and Fundamentals. SAS System Options: The True Heroes of Macro Debugging Kevin Russell and Russ Tyndall, SAS Institute Inc.

The Basics of PROC FCMP. Dachao Liu Northwestern Universtiy Chicago

PharmaSUG 2013 CC26 Automating the Labeling of X- Axis Sanjiv Ramalingam, Vertex Pharmaceuticals, Inc., Cambridge, MA

CHAPTER 7 Using Other SAS Software Products

Tales from the Help Desk 5: Yet More Solutions for Common SAS Mistakes Bruce Gilsen, Federal Reserve Board

SAS Institute Exam A SAS Advanced Programming Version: 6.0 [ Total Questions: 184 ]

An Introduction to SAS Macros

Macro to compute best transform variable for the model

Using Templates Created by the SAS/STAT Procedures

Helping You C What You Can Do with SAS

A Format to Make the _TYPE_ Field of PROC MEANS Easier to Interpret Matt Pettis, Thomson West, Eagan, MN

PharmaSUG China Paper 059

Going Under the Hood: How Does the Macro Processor Really Work?

ABSTRACT MORE THAN SYNTAX ORGANIZE YOUR WORK THE SAS ENTERPRISE GUIDE PROJECT. Paper 50-30

%MAKE_IT_COUNT: An Example Macro for Dynamic Table Programming Britney Gilbert, Juniper Tree Consulting, Porter, Oklahoma

SAS Data Integration Studio Take Control with Conditional & Looping Transformations

Validation Summary using SYSINFO

A Practical and Efficient Approach in Generating AE (Adverse Events) Tables within a Clinical Study Environment

My Reporting Requires a Full Staff Help!

APPENDIX 2 Customizing SAS/ASSIST Software

Using PROC REPORT to Cross-Tabulate Multiple Response Items Patrick Thornton, SRI International, Menlo Park, CA

Greenspace: A Macro to Improve a SAS Data Set Footprint

QUEST Procedure Reference

SAS Programming Techniques for Manipulating Metadata on the Database Level Chris Speck, PAREXEL International, Durham, NC

A Comparison of the LUA Procedure and the SAS Macro Facility

ABSTRACT INTRODUCTION MACRO. Paper RF

SAS Drug Development Program Portability

Useful Tips When Deploying SAS Code in a Production Environment

CMISS the SAS Function You May Have Been MISSING Mira Shapiro, Analytic Designers LLC, Bethesda, MD

Base and Advance SAS

Different Methods for Accessing Non-SAS Data to Build and Incrementally Update That Data Warehouse

Surviving the SAS Macro Jungle by Using Your Own Programming Toolkit Kevin Russell, SAS Institute Inc., Cary, North Carolina

INTRODUCTION TO PROC SQL JEFF SIMPSON SYSTEMS ENGINEER

Macros from Beginning to Mend A Simple and Practical Approach to the SAS Macro Facility

Automated Checking Of Multiple Files Kathyayini Tappeta, Percept Pharma Services, Bridgewater, NJ

Paper HOW-06. Tricia Aanderud, And Data Inc, Raleigh, NC

Unlock SAS Code Automation with the Power of Macros

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

An Efficient Method to Create Titles for Multiple Clinical Reports Using Proc Format within A Do Loop Youying Yu, PharmaNet/i3, West Chester, Ohio

10 The First Steps 4 Chapter 2

Quick and Efficient Way to Check the Transferred Data Divyaja Padamati, Eliassen Group Inc., North Carolina.

Get SAS sy with PROC SQL Amie Bissonett, Pharmanet/i3, Minneapolis, MN

A Macro to Create Program Inventory for Analysis Data Reviewer s Guide Xianhua (Allen) Zeng, PAREXEL International, Shanghai, China

The Dataset Diet How to transform short and fat into long and thin

A Quick and Gentle Introduction to PROC SQL

Tales from the Help Desk 6: Solutions to Common SAS Tasks

A SAS Macro to Generate Caterpillar Plots. Guochen Song, i3 Statprobe, Cary, NC

A SAS Macro Utility to Modify and Validate RTF Outputs for Regional Analyses Jagan Mohan Achi, PPD, Austin, TX Joshua N. Winters, PPD, Rochester, NY

Get Started Writing SAS Macros Luisa Hartman, Jane Liao, Merck Sharp & Dohme Corp.

A Time Saver for All: A SAS Toolbox Philip Jou, Baylor University, Waco, TX

What s New in 9.3. Rick Langston, SAS Institute Inc. Transforming the World. Copyright 2011, SAS Institute Inc. All rights reserved.

In this paper, we will build the macro step-by-step, highlighting each function. A basic familiarity with SAS Macro language is assumed.

KEYWORDS Metadata, macro language, CALL EXECUTE, %NRSTR, %TSLIT

Creating and Executing Stored Compiled DATA Step Programs

Files Arriving at an Inconvenient Time? Let SAS Process Your Files with FILEEXIST While You Sleep

Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA

Hidden in plain sight: my top ten underpublicized enhancements in SAS Versions 9.2 and 9.3

The GEOCODE Procedure and SAS Visual Analytics

Customized Flowcharts Using SAS Annotation Abhinav Srivastva, PaxVax Inc., Redwood City, CA

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

Building Sequential Programs for a Routine Task with Five SAS Techniques

Virtual Accessing of a SAS Data Set Using OPEN, FETCH, and CLOSE Functions with %SYSFUNC and %DO Loops

SAS Macro Language: Reference

Chapter 28 Saving and Printing Tables. Chapter Table of Contents SAVING AND PRINTING TABLES AS OUTPUT OBJECTS OUTPUT OBJECTS...

Now That You Have Your Data in Hadoop, How Are You Staging Your Analytical Base Tables?

Exploring the SAS Macro Function %SYSFUNC

Acknowledgments xi Preface xiii About the Author xv About This Book xvii New in the Macro Language xxi

Using Different Methods for Accessing Non-SAS Data to Build and Incrementally Update That Data Warehouse

Using a Picture Format to Create Visit Windows

= %sysfunc(dequote(&ds_in)); = %sysfunc(dequote(&var));

Data Edit-checks Integration using ODS Tagset Niraj J. Pandya, Element Technologies Inc., NJ Vinodh Paida, Impressive Systems Inc.

PharmaSUG China. Systematically Reordering Axis Major Tick Values in SAS Graph Brian Shen, PPDI, ShangHai

The correct bibliographic citation for this manual is as follows: SAS Institute Inc Proc EXPLODE. Cary, NC: SAS Institute Inc.

SAS Macro. SAS Training Courses. Amadeus Software Ltd

ABSTRACT: INTRODUCTION: WEB CRAWLER OVERVIEW: METHOD 1: WEB CRAWLER IN SAS DATA STEP CODE. Paper CC-17

Introduction to the SAS Macro Facility

Leveraging SAS Visualization Technologies to Increase the Global Competency of the U.S. Workforce

APPENDIX 4 Migrating from QMF to SAS/ ASSIST Software. Each of these steps can be executed independently.

Using Recursion for More Convenient Macros

Reading in Data Directly from Microsoft Word Questionnaire Forms

Using Data Transfer Services

work.test temp.test sasuser.test test

SURVIVING THE SAS MACRO JUNGLE BY USING YOUR OWN PROGRAMMING TOOLKIT

Transcription:

ABSTRACT PharmaSUG 2013 - Paper AD24-SAS Submitting SAS Code On The Side Rick Langston, SAS Institute Inc., Cary NC This paper explains the new DOSUBL function and how it can submit SAS code to run "on the side" while your DATA step is still running. It also explains how this function differs from invoking CALL EXECUTE or invoking the RUN_COMPILE function of FCMP. Several examples are shown that introduce new ways of writing SAS code. INTRODUCTION The DOSUBL function is new in the second maintenance release of SAS 9.3. The function submits code to SAS and waits for that code to complete submission. The DOSUBL function has a single argument, which is a string value. For example: rc = dosubl('data x; y=1; %let abc=1;'); abc=symget('abc'); put abc=; set x; put _all_; Although this code is not of much value, it shows the syntax of the function. The small DATA step that creates Work.X is submitted and run to completion before the outer DATA step is completed. All macro variables, macro definitions, and Work data sets are available to the code that is to be executed. All macro variables, macro definitions, and Work data sets created by the executed code are then subsequently available to your DATA step and any SAS code that follows. For example, in the SAS code above, the data set Work.X and the macro variable &abc are available after the DOSUBL function completes. THE DIFFERENCE BETWEEN DOSUBL AND CALL EXECUTE DOSUBL differs from CALL EXECUTE in that CALL EXECUTE performs only immediate execution on macro code. If that macro code expands to DATA and PROC steps, that code is stacked to execute after the current DATA step completes. Consider this code: %macro doit(value); call symput('xyz',"&value."); %mend doit; %let xyz=q; call execute('%doit(a)'); xyz_value = symget('xyz'); put xyz_value=; When this code runs, the DATA step gives us this output: xyz_value=q The output shows that the DATA step with the SYMPUT call in it has been stacked to run later, so the value of &xyz is still q. But, if we run this code instead, we get the desired result: %let xyz=q; 1

rc = dosubl('%doit(b)'); xyz_value = symget('xyz'); put xyz_value=; We get this result because the %doit macro has run to completion before returning from the DOSUBL function: xyz_value=b THE DIFFERENCE BETWEEN DOSUBL AND RUN_MACRO Another function that is similar to DOSUBL is RUN_MACRO. The RUN_MACRO function runs within the compiler (FCMP) environment, and cannot run in any other environment. Also, RUN_MACRO is intended specifically for invoking macros (hence its name), but the DOSUBL function can execute any code provided to it. It is more straightforward to invoke macros using DOSUBL. Consider this example. The macro %testmacro expects to use two macro variables, &a and &b, and the result of subtracting them is saved in macro variable &p. To invoke this macro using PROC FCMP, you call RUN_MACRO and pass the name of the macro and the arguments whose names are used as the names of the macros containing the values. /* Create a macro called TESTMACRO. */ %macro testmacro; %let p = %sysevalf(&a - &b); %mend testmacro; /* Use TESTMACRO within a function in PROC FCMP to subtract two numbers. */ proc fcmp outlib = sasuser.ds.functions; function subtract_macro(a, b); rc = run_macro('testmacro', a, b, p); if rc eq 0 then return(p); else return(.); endsub; To call the macro, you do so via the function that has been defined: /* Make a call from the DATA step. */ option cmplib = (sasuser.ds); a = 5.3; b = 0.7; p =.; p = subtract_macro(a, b); put p=; Resulting in this output: p=4.6 Compare using the FCMP environment with using DOSUBL, where you can call the macro directly: %global a b p; %put p=&p; /* should not yet be known */ %let a=5.3; %let b=0.7; rc = dosubl('%testmacro'); %put p=&p; Also resulting in the same output: p=4.6 2

USING DOSUBL WITHIN PROC STREAM DOSUBL can be very handy when it is used in the new STREAM procedure, which is experimental in SAS 9.3 and production in SAS 9.4. The STREAM procedure enables you to process an input stream that consists of arbitrary text that can contain SAS macro specifications. First consider this macro: %macro setval(x); %global y; y = &x + 2; call symputx('y',y); %mend setval; %let given=3; We want to generate output from PROC STREAM, where we substitute macro variables and computed values: filename mytext temp; proc stream outfile=mytext; begin I was given &given apples and I added 2 more of them and my new amount was %setval(&given) &y. ;;;; When this code runs, we get the following in the mytext file: I was given 3 apples and I added 2 more of them and my new amount was y = 3 + 2; call symputx('y',y); We did not want to see the generated SAS code, just the result of the computation in the macro variable. Using DOSUBL with %SYSFUNC, we can do this successfully: %let given=3; filename mytext temp; proc stream outfile=mytext; begin I was given &given apples and I added 2 more of them and my new amount was %let x=%sysfunc(dosubl(%setval(&given))); &y.. ;;;; This results in this text in the mytext file: I was given 3 apples and I added 2 more of them and my new amount was 5. The executed code is not part of the output stream because it is being executed "on the side". USING DOSUBL WITH PROC SQL Here is another example to compare CALL EXECUTE with DOSUBL. This example invokes PROC SQL and its ability to create macro variables. We would like to use those macro variables within the same DATA step. The very same PROC SQL code is passed both to CALL EXECUTE and to DOSUBL. First we see the SAS log where we create data set Work.Temp with two observations each for the values of 1 through 5, for the variable i: 1 data temp; 2 do i=1 to 5; 3 output; output; 4 end; 5 NOTE: The data set WORK.TEMP has 10 observations and 1 variables. 3

We can run PROC SQL and use the COUNT function with the INTO clause to create macro variables &n and &dist, which hold the observation count and the distinct value count respectively: 6 proc sql; 7 select 8 count(i) into :n from temp; 9 select 10 count(distinct i) into :dist from temp; 11 quit; 12 13 %put count is &n with &dist distinct values ; count is 10 with 5 distinct values 14 We try invoking the same code with CALL EXECUTE, changing the names of the macro variables to avoid confusion. We attempt, within the same DATA step, to access the &n and &distmacro variables via SYMGET. But, we find they are not yet available. The PROC SQL step executes after our current DATA step completes, as we see in the SAS log: 15 16 call execute(' 17 proc sql; 18 select 19 count(i) into :n2 from temp; 20 select 21 count(distinct i) into :dist2 from temp; 22 quit; 23 '); 24 n2 = input(symget('n2'),best12.); 25 dist2 = input(symget('dist2'),best12.); 26 put n2= dist2=; 27 NOTE: Invalid argument to function SYMGET at line 24 column 17. NOTE: Invalid argument to function SYMGET at line 25 column 20. n2=. dist2=. n2=. dist2=. _ERROR_=1 _N_=1 NOTE: CALL EXECUTE generated line. 1 + proc sql; 1 + select count(i) into :n2 from temp; 1 + select count(distinct i) into :dist2 from temp; 1 + quit; Now, we try the invoking the same code using DOSUBL, changing the names of the macro variables to avoid confusion. This time we are able to obtain the macro variables because the PROC SQL code executed on the side while our DATA step awaited the return from DOSUBL. 28 29 30 rc = dosubl(' 31 proc sql; 32 select 33 count(i) into :n3 from temp; 34 select 35 count(distinct i) into :dist3 from temp; 36 quit; 37 '); 38 n3 = input(symget('n3'),best12.); 4

39 dist3 = input(symget('dist3'),best12.); 40 put n3= dist3=; 41 n3=10 dist3=5 CODE SIMPLIFICATION In the paper Use the Full Power of SAS in Your Function-Style Macros (Rhoads 2012), there are several examples using the RUN_MACRO function. Michael Rhoads describes the three pieces of SAS code - the outer macro, the user-written function, and the inner macro - as a way to harness the power of function-style macros. However, using DOSUBL, this becomes much simpler with only one section of SAS code needed. In Michael s first example that is described in the paper, the three sections of code can be reduced to one section using DOSUBL, as seen below: %MACRO ExpandVarList(data=_LAST_, var=_all_); %if %upcase(%superq(data)) = _LAST_ %then %let data = &SYSLAST; %let rc = %sysfunc(dosubl(%str( proc transpose data=&data(obs=0) out=expandvarlist_temp; var &VAR; proc sql noprint; select _name_ into :temp_varnames separated by ' ' from ExpandVarList_temp ; drop table ExpandVarList_temp; quit ))); &temp_varnames %MEND ExpandVarList; CONCLUSION The DOSUBL function is a very powerful addition to your arsenal of SAS functions. Being able to execute SAS code on the side allows for you to perform arbitrary functionality and react to results without having to resort to more complex SAS coding. REFERENCES Rhoads, Michael. 2012. Use the Full Power of SAS in Your Function-Style Macros. NorthEast SAS Users Group 2012, Baltimore, Maryland. Available at www.nesug.org/proceedings/nesug12/bb/bb14.pdf. CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author: Rick Langston SAS Campus Drive SAS Institute Inc. Cary, NC 27513 E-mail: Rick.Langston@sas.com SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies. 5