The Basics of PROC FCMP. Dachao Liu Northwestern Universtiy Chicago

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

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

Helping You C What You Can Do with SAS

Functions vs. Macros: A Comparison and Summary

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

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

TIPS AND TRICKS: IMPROVE EFFICIENCY TO YOUR SAS PROGRAMMING

Submitting SAS Code On The Side

Data Manipulation with SQL Mara Werner, HHS/OIG, Chicago, IL

Using a Picture Format to Create Visit Windows

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

Imelda C. Go, South Carolina Department of Education, Columbia, SC

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Excel Lesson 3 USING FORMULAS & FUNCTIONS

Are you Still Afraid of Using Arrays? Let s Explore their Advantages

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

A Quick and Gentle Introduction to PROC SQL

What s New in SAS Studio?

Let Hash SUMINC Count For You Joseph Hinson, Accenture Life Sciences, Berwyn, PA, USA

Unit E Step-by-Step: Programming with Python

Paper ###-YYYY. SAS Enterprise Guide: A Revolutionary Tool! Jennifer First, Systems Seminar Consultants, Madison, WI

Syntax Conventions for SAS Programming Languages

Square Peg, Square Hole Getting Tables to Fit on Slides in the ODS Destination for PowerPoint

How to Incorporate Old SAS Data into a New DATA Step, or What is S-M-U?

IF there is a Better Way than IF-THEN

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Copy That! Using SAS to Create Directories and Duplicate Files

My Reporting Requires a Full Staff Help!

Unlock SAS Code Automation with the Power of Macros

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

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

Intro. Scheme Basics. scm> 5 5. scm>

How to Create Data-Driven Lists

SAS 9 Programming Enhancements Marje Fecht, Prowerk Consulting Ltd Mississauga, Ontario, Canada

Getting it Done with PROC TABULATE

Using Templates Created by the SAS/STAT Procedures

DATA Step in SAS Viya : Essential New Features

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

Fifteen Functions to Supercharge Your SAS Code

Streamline Table Lookup by Embedding HASH in FCMP Qing Liu, Eli Lilly & Company, Shanghai, China

Advanced Programming Techniques Using the DS2 Procedure

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Using SAS with Oracle : Writing efficient and accurate SQL Tasha Chapman and Lori Carleton, Oregon Department of Consumer and Business Services

Enterprise Client Software for the Windows Platform

SAS Online Training: Course contents: Agenda:

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

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

A Generalized Macro-Based Data Reporting System to Produce Both HTML and Text Files

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

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

9 Ways to Join Two Datasets David Franklin, Independent Consultant, New Hampshire, USA

CHAPTER 7 Using Other SAS Software Products

Chapter 2 Basic Elements of C++

DOWNLOAD PDF MICROSOFT EXCEL ALL FORMULAS LIST WITH EXAMPLES

Data Representation. Variable Precision and Storage Information. Numeric Variables in the Alpha Environment CHAPTER 9

Tweaking your tables: Suppressing superfluous subtotals in PROC TABULATE

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

Using Metadata Queries To Build Row-Level Audit Reports in SAS Visual Analytics

Can you decipher the code? If you can, maybe you can break it. Jay Iyengar, Data Systems Consultants LLC, Oak Brook, IL

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

HOW TO DEVELOP A SAS/AF APPLICATION

Variables and numeric types

Merging Data Eight Different Ways

How to approach a computational problem

SAS ENTERPRISE GUIDE USER INTERFACE

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

Anyone Can Learn PROC TABULATE, v2.0

Formulas, LookUp Tables and PivotTables Prepared for Aero Controlex

Easing into Data Exploration, Reporting, and Analytics Using SAS Enterprise Guide

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

printf( Please enter another number: ); scanf( %d, &num2);

Getting Up to Speed with PROC REPORT Kimberly LeBouton, K.J.L. Computing, Rossmoor, CA

Using the SQL Editor. Overview CHAPTER 11

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

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

How to Implement the One-Time Methodology Mark Tabladillo, Ph.D., Atlanta, GA

Unit 7. Functions. Need of User Defined Functions

ABSTRACT INTRODUCTION THE ODS TAGSET FACILITY

Computer Science Lab Exercise 1

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

An Application of PROC NLP to Survey Sample Weighting

SIMPLE INPUT and OUTPUT:

DBLOAD Procedure Reference

External Files. Definition CHAPTER 38

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

Procedures (Subroutines) and Functions

Teacher Activity: page 1/9 Mathematical Expressions in Microsoft Word

Unit 3. Operators. School of Science and Technology INTRODUCTION

An Application of ODS Tagsets. Notice! Paper

Programming Beyond the Basics. Find() the power of Hash - How, Why and When to use the SAS Hash Object John Blackwell

Using Dynamic Data Exchange

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

SAS Visual Analytics Environment Stood Up? Check! Data Automatically Loaded and Refreshed? Not Quite

Lecture 5: Methods CS2301

Using Data Set Options in PROC SQL Kenneth W. Borowiak Howard M. Proskin & Associates, Inc., Rochester, NY

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

Language Reference Manual

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Use of Technology. Perform statistical computations on stored data or entered statistics.

How to Set up a Budget Advanced Excel Part B

Transcription:

The Basics of PROC FCMP Dachao Liu Northwestern Universtiy Chicago ABSTRACT SAS Functions can save SAS users time and effort in programming. Each release of SAS has new functions added. Up to the latest version of SAS 9.3, there are over 450 functions. Even with a vast plethora of SAS functions, some SAS users still find the lack of some functions to perform certain tasks. Fortunately, In SAS 9, a new procedure called PROC FCMP was developed to help SAS users to write their own functions. This paper will discuss the basics of PROC FCMP and how we can use it to build our own SAS functions. The intended audience would be those who have a basic idea of SAS functions. INTRODUCTION Like many other programming languages, SAS has a vast number of built-in functions. A SAS function is a method to perform a task like computation or data manipulation and return a value based on the arguments given. SAS is very rich in built-in functions, saving SAS users tons of time and programming effort. In SAS version 6, there were only 236 functions, In SAS 9, there are almost twice as many. But oftentimes, we still find there is not enough. How can we do this special computation? How can we remove that extra character? How can we use MS Excel functions in SAS? The questions can go on and on. To meet SAS users needs, SAS institute developed a new procedure, PROC FCMP. By using PROC FCMP, we can find answers to those questions. In this paper, I will discuss what is PROC FCMP, and then some examples. DISCUSSION What is a function? In our daily life, by saying something is functioning, we mean something is doing task by rule. The heart is functioning when it plumps blood through the body. In academic field, we also use function to mean performing some tasks by rule, thus creating lots of functions in math and computer languages. They are like machines because they need an input to create an output. So the output is related somehow to the input. Y=9*x is a function. If the input is 8 for x, then the output is 72 for y, and we may write f(8) = 72. The input variable(s) are sometimes referred to as the argument(s) of the function. Like a math function, a SAS function also needs an input in order to create an output. A SAS function is a method to perform a task like computation or data manipulation and return a value based on the arguments given. SAS functions are often categorized by the type of data to be processed and type of task a function performs. SAS data sets can be classified as numeric and character, therefore SAS functions are often categorized into function handling numeric data (numeric functions) and function handling character data (character functions) and some special functions. Special functions perform special tasks like converting data from one data type to another. Numeric functions can do arithmetic, mathematical, trigonometric, statistical, financial computations, generate random numbers. Character functions perform tasks like searching for strings, replacing strings or joining strings together, extracting a portion of a character value, counting how many words in a sentence. But sometimes, SAS users still cannot perform what they want by using the existing functions. For example, we can use tranwrd()to delete some characters in a string. Suppose we have a string name1, name2, name3, name4, name5, name6, somehow the string was entered like this name1,name2,,,name3,, name4,name5,,,,name6 we want to get rid of the extra, then tranwrd() only cannot do the job. But with the help of PROC FCMP, we can. What is PROC PCMP? PROC FCMP is a gift to SAS users who can use it to write their own functions and CALL routines or subroutines using DATA step syntax. Why it is a gift? Because it is a Base SAS procedure and you don t need to spend extra money on it as long as you have Base SAS. FCMP routines are stored in a SAS catalog and can be called from several SAS/STAT, SAS/ETS, and SAS/OR procedures. In SAS 9.2, FCMP routines can be called from DATA step. SAS users can use the SAS Function Compiler (FCMP) procedure to create, and store SAS functions, and subroutines before using them in other SAS procedures or DATA steps. Most features of the SAS programming language can be used in functions and subroutines. PROC FCMP functions and subroutines can be called from the DATA step just as any other SAS function, or subroutine, making it easier for SAS users to read, write, and maintain complex code with independent and reusable subroutines. The PROC FCMP routines can be reused in any DATA 1

step or SAS procedures. And the procedures include GENMOD, MCMC, NLMIXED, PHREG etc. even the Output Delivery System (ODS) Functions are independent computational blocks that require zero or more arguments. A subroutine is a special type of function that has no return value. The function begins with the FUNCTION statement, and the subroutine begins with the SUBROUTINE statement. A single FCMP procedure step can contain several functions and subroutines. The basic syntax is: PRCO FCMP OUTLIB=libname.dataset.package; Function body ; RETURN(expression); ENDSUB; or Subroutine body ; RETURN; ENDSUB; OUTLIB specifies the three-level name of an output data set to which the compiled functions and subroutines are written when the PROC FCMP step ends. And the functions and subroutines are saved in an output library. If the OUTLIB= is not used, then no functions and subroutines that are declared in the current PROC FCMP step are saved. A function body contains a function name, its arguments, and data step (code). The arguments are passed by value, which means that the value of the actual argument, variable, or value that is passed to the function from the calling environment is copied before being used by the function. This copying ensures that any modification of the formal argument by the function does not change the original value. Data step code specifies the tasks to be performed by the function. The RETURN statement is used to return a value to a function. The RETURN statement uses an expression enclosed in parentheses, and contains the value that is returned to the calling environment. The function declaration ends with an ENDSUB statement. The subroutine begins with the SUBROUTINE keyword instead of the FUNCTION keyword. A subroutine body contains a subroutine name, its arguments, and data step (code). The arguments are passed by reference and are listed in the OUTARGS statement, which means that any modification of the argument will modify the original variable that was passed. A RETURN statement in a subroutine does not return a value. Data step code specifies the tasks to be performed by the subroutine. Unlike other SAS procedures or DATA steps, PROC FCMP doesn t take in data by using a data statement nor output data by using output statement. Data is typically transferred into and out of PROC FCMP routines by using parameters. In general, PUT statement can be used to see the results in the log. Now let s come to some examples, 2

Suppose we have survey data consisting of 9 questions with values from 1 to 4 each, measuring trust in 9 different things, 1 stands for A lot, 2 some, 3 a little and 4 not at all. Suppose we want to create a new variable TRUST with value of 1(trust, not 4) and 0(not trust, only 4) then we use proc fcmp outlib=sasuser.funcs.trial; function COMP(Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9); COMP= (sum ((Q1 le 3), (Q2 le 3), (Q3 le 3), (Q4 le 3), (Q5 le 3), (Q6 le 3), (Q7 le 3), (Q8 le 3), (Q9 le 3)) = 9); return(comp); endsub; options cmplib=sasuser.funcs; data two; set one; TRUST = comp(q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9); run; outlib=sasuser.funcs.trial; Use this option when you want to save functions and subroutines in an output library. Otherwise functions and subroutines would not be saved.the function in the example is saved to the data set sasuser.funcs inside a package called trial. A package is a collection of related routines that are specified by the user. function COMP(Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9); This statement says the function will perform some operations on this nine variables and the name of operations is called COMP. COMP=(sum ((Q1 le 3), (Q2 le 3), (Q3 le 3), (Q4 le 3), (Q5 le 3), (Q6 le 3), (Q7 le 3), (Q8 le 3), (Q9 le 3)) = 9); This says what exactly the function will be doing. Q le 3 is a logical statement and it returns either 1 if the statement is true: that is Q is less than 3 or 0 if the statement is false: that is Q is not less than 3. If these 9 logical statements are all true, then the sum will be 9, making COMP =1; If one of these 9 logical statements are not true, then the sum will not be 9, making COMP =0. return(comp) will return the value of COMP to the calling environment. endsub is used to close the function. Before the data step, cmplib=sasuser.funcs is specified to tell SAS where to look for the function of COMP. In the data step, the function is called. And the variable TRUST takes the values from COMP. The second example: Suppose we have a string name1,name2,,,name3,, name4,name5,,,,name6 but we only want one comma to separate each name name1, name2, name3, name4, name5, name6, then we can get it done by using proc fcmp outlib=work.myfuncs.char; subroutine decomma(string $); outargs string; string=tranwrd(string,",,",","); if index(string,",,") > 0 then call decomma(string); endsub; quit; options cmplib=work.myfuncs; data _null_; list="namel,name2,,name3,,,name4,name5,,name6,,,,,namex"; call decomma(list); 3

put list=; run; outlib=work.myfuncs.char; Use this option when you want to save functions and subroutines in an output library. Otherwise functions and subroutines would not be saved. The subroutine in the example is saved to the data set work.myfuncs inside a package called char. A package is a collection of related routines that are specified by the user. subroutine decomma(string $); This statement says the subroutine will perform some operations on this string and the name of operations is call decomma. Since it is a string, a $ sign must be used. outargs string; After subroutine statement, outargs must be used and the argument in outargs is returned through a reference. string=tranwrd(string,",,",","); if index(string,",,") > 0 then call decomma(string); This says what exactly the subroutine will be doing. TRANWRD function is used to remove repeated commas in text. index(string,",,") > 0 says,, is found. call decomma(string) says remove it. Endsub It is used to close the function. Before the data step, cmplib=work.myfuncs is specified to tell SAS where to look for the subroutine of decomma. In the data step, the subroutine is called to perform the task of removing the extra comma. You can see the results by issuing a PUT statement. The third example: SAS doesn t have some functions that MS Excel has. But they are available by using PROC FCMP and you will find that MS Excel has a vast repertoire of functions. By issuing the following code, those excel functions become handy. PROC FCMP INLIB=sashelp.slkwxl LISTALL; RUN; Here is the list of the functions: Function avedev_slk ( data ) Function devsq_slk ( data ) Function varp_slk ( data ) Function even_slk ( x ) Function factdoule_silk ( x ) Function floor_slk ( n, sg ) Function multinormial_slk ( nums ) Function odd_slk ( x ) Function product_slk ( nums ) Function accrint_slk ( issue, finterest, settlement, rate, par, freq, basis ) Function accrintm_slk ( issue, maturity, rate, par, basis ) Function amordegrc_slk ( cost, datep, fperiod, salvage, period, rate,basis ) Function amorlinc_slk ( cost, datep, fperiod, salvage, period, rate, basis ) Function coupdaybs_slk ( settlement, maturity, freq, basis ) Function coupdays_slk ( settlement, maturity, freq, basis ) Function coupdaysnc_slk ( settlement, maturity, freq, basis ) Function coupncd_slk ( settlement, maturity, freq, basis ) Before you use the excel functions in SAS, you should specify cmplib=sasuser.funcs to tell SAS where to look for the excel function. In the data step, the function is called. For example, you can call product_skl function. The product function multiplies all the numbers given as arguments and returns the product. In SAS, the arguments must take an array in product function. OPTIONS CMPLIB = sashelp.slkwxl; 4

DATA _NULL_; array x[5] (4 5 3 2 6); product = product_slk(x); PUT 'Product is ' product; RUN; You will see Product is 720 in the log. These three examples have given you an idea about the basics of PROC FCMP. You definitely can create your own functions and make use of Excel functions in SAS now. CONCLUSION This paper covers the very basics of the FCMP procedure. There are more topics about the FCMP procedure, like function editor, SELECT statements in PROC SQL, matrix operation, calling C function, etc. Readers can go to reference material listed below for further study. With the FCMP procedure, you can get more functionalities out of SAS and solve problems otherwise difficult or impossible. REFERENCE SAS Institute Inc. 2003. The FCMP Procedure. Cary, NC: SAS Institute Inc. http://support.sas.com/documentation/onlinedoc/base/91/fcmp.pdf Secosky, Jason, 2007, User-Written DATA Step Functions, published in the Proceedings of the SAS Global Forum 2007 Conference, Cary, NC: SAS Institute Inc., CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Dachao Liu Northwestern University Suite 1400 680 N Lake Shore Dr. Chicago, IL 60611 Phone (312)503-2809 Email: dachao-liu@northwestern.edu 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