Using SAS PROC FCMP Cheryl Xiyun Wang Statistics Canada November 14 2012 Using SAS PROC FCMP 1. Introduction 2. Examples of Using PROC FCMP 3. Using PROC FCMP to Build Customized SAS Functions in G-Sam 4. DATA Step Runs Another DATA Step Through PROC FCMP 5. Other Features in PROC FCMP 6. References 7. Questions Page 2 Statistics Canada • Statistique Canada 13/11/2012 1. Introduction 1) What is PROC FCMP? PROC FCMP is the SAS Function Compiler procedure; it enables us to create and test customized “functions/CALL routines” using DATA step syntax and store them in SAS packages to be shared; Some PROC FCMP statements are slightly different from DATA step statements and we can use most features of SAS programming language to create PROC FCMP “functions/CALL routines” (variables, constants, control flows, math operations...); Just like any other SAS “functions/CALL routines”, PROC FCMP “functions/CALL routines” can be used in DATA step, WHERE statement, Output Delivery System (ODS), and many SAS procedures (although not all); Page 3 Statistics Canada • Statistique Canada 13/11/2012 1. Introduction (cont’d) 2) Advantages of using PROC FCMP A library of reusable “functions/ CALL routines” can be built using DATA step syntax, which can be reused in many other SAS programs; Complex programs can be simplified by abstracting/wrapping common computations into PROC FCMP “functions/ CALL routines”; PROC FCMP “functions/CALL routines” are independent from their use which means any SAS program calling PROC FCMP functions/CALL routines is not affected by the implementation of PROC FCMP functions/CALL routines; (comparing to macros) Developers can more easily read, write, and maintain complex code with independent and reusable functions/CALL routines; Page 4 Statistics Canada • Statistique Canada 13/11/2012 1. Introduction (cont’d) 3) PROC FCMP Syntax PROC FCMP options; (declare statements; program statements;) ABORT; ARRAY name[dimensions] </NOSYMBOLS | variables | constants | (initial-values)>; ATTRIB variables <FORMAT=format-name LABEL='label' LENGTH=length>; LABEL variable='label'; STRUCT structure-name variable; DELETEFUNC function-name; DELETESUBR subroutine-name; FUNCTION function-name(argument-1, ..., argument-n) options SUBROUTINE subroutine-name (argument-1, ..., argument-n) options OUTARGS out-argument-1, ..., out-argument-n; LISTFUNC function-name; LISTSUBR subroutine-name; QUIT; Page 5 Statistics Canada • Statistique Canada 13/11/2012 1. Introduction (cont’d) 4) Declare PROC FCMP Functions and CALL Routines Function name(argument-1, ... , argument-n); program-statements; return (expression); endsub; Subroutine name(argument-1, ..., argument-n); outargs out-argument-1, ..., out-argument-N; program-statements; return; endsub; Here program-statements are built using SAS programming languages Page 6 Statistics Canada • Statistique Canada 13/11/2012 2. Examples of using PROC FCMP 1) Simple examples of using PROC FCMP functions/CALL routines Demo! 2) An example of using PROC FCMP in functions in PROC OPTMODEL Demo! Page 7 Statistics Canada • Statistique Canada 13/11/2012 3. Customized Functions in G-Sam 1) Situation A group of complex mathematical functions are used many times in our large optimization problems which are to be implemented by SAS PROC OPTMODEL; 2) Solution Using PROC FCMP to build customized SAS functions for those complex mathematical functions; Using these customized SAS Functions in any places in SAS PROC OPTMODEL; Page 8 Statistics Canada • Statistique Canada 13/11/2012 3. Customized Functions in G-Sam (Cont’d) 3) An example of G-Sam functions and their usage (Demo) Page 9 Statistics Canada • Statistique Canada 13/11/2012 3. Customized Functions in G-Sam (Cont’d) 4) Benefit of Using PROC FCMP in G-Sam Perfect fit; wrap complex computations into independent functions and plug them into PROC OPTMODEL easily; eases our system implementation; Work of building PROC FCMP functions be done independently by another developer (parallel); efficient Easy maintenance: from times to time, specs changes a lot but are mostly in these functions, thus most system modifications were done in the FCMP functions; less impact; Numeric precision needs special attentions. Don’t use customized functions in PROC Optmodel variables; Page 10 Statistics Canada • Statistique Canada 13/11/2012 4. Run data step within another data step through PROC FCMP 1) PROC FCMP run_macro(…) function; rc = RUN_MACRO ('macro_name', variable_1, variable_2, ..., variable_n) ; where: the return code rc indicates that the macro call was attempted; variable_1, variable_2, ..., variable_n are PROC FCMP variables to be passed to macro variables in the invoked SAS Macro; then invoked SAS Macro can execute properly. 2) Demo Page 11 Statistics Canada • Statistique Canada 13/11/2012 5. Other features in PROC FCMP 1) SAS FCMP Function Editor SAS 9.2 / SAS 9.3 -> Solutions -> Analysis -> FCmp Function Editor Browse / edit all available PROC FCMP functions/CALL routines; Syntax checking; testing; manage datasets and logs ; 2) Enables calling c/c++ functions from Data Step Data Step -> PROC FCMP wrapper functions –> Proc proto functions –> c/c++ functions Good paper: Developing User-Defined Functions in SAS®: A Summary and Comparison 3) Build-in Microsoft Excel functions 4) Build-in functions for Matrix operations Page 12 Statistics Canada • Statistique Canada 13/11/2012 6. References SAS 9.3 installed locally, use SAS 9.3 Help Files Contents -> SAS Products -> Base SAS -> Base SAS 9.3 Procedures Guide -> Procedures -> FCMP Procedure / FCMP Special Functions and Call Routines / FCmp Function Editor SAS 9.3 not installed, Check SAS online document: PROC FCMP :: Base SAS(R) 9.3 Procedures Guide, Second Edition Developing User-Defined Functions in SAS®: A Summary and Comparison (comparing Macro, SAS/IML, SCL, Proc Proto and Proc FCMP: how to build and performance) Functioning at an Advanced Level: PROC FCMP and PROC PROTO (simple examples) Overview of the SAS Function Compiler (FCMP – GASUG) Top 10 reasons for a modeller to learn PROC FCMP (Financial market analyst): Google “PROC FCMP” Difference between proc fcmp and datastep Page 13 Statistics Canada • Statistique Canada 13/11/2012 7. Questions Yves Deguire Systems Chief, SAS Technology Center, Statistics Canada, 150 Tunney's Pasture Driveway Ottawa, Ontario, K1A 0T6 Yves.Deguire@a.statcan.gc.ca (613) 951-1282 Cheryl Wang IT Team Leader, SAS Technology Center, Statistics Canada, 150 Tunney's Pasture Driveway Ottawa, Ontario, K1A 0T6 Cherylxiyun.wang@a.statcan.gc.ca (613) 951-0843 Page 14 Statistics Canada • Statistique Canada 13/11/2012