SAS Monotonic() function to create observation number in Proc SQL…
In SQL, there is no concept of an observation number in tables, as there is when using Data step (i.e....
Have you ever wondered, Why SAS never had functions similar to any other programming Language i.e. C, C++, Java…
LINK/ Return statement and SAS Macros to some extent served the purpose in modularizing and developing re-usable code for repeating tasks…but doesn’nt make it is easy to follow….Macros are easier to reuse; however, they are not independent from the main program, they involve non-DATA step syntax, and substantial use of macros can result in illegible code.
SAS finally broke this jinx with FCMP procedure in version 9.1 and above.
In SASĀ®9.1, the FCMP procedure provides the ability to write functions to be used in conjunction with few SAS Procedures….
SAS 9.1 Proc FCMP Documentation page – http://support.sas.com/documentation/onlinedoc/base/91/fcmp.pdf#navpanes=0
Starting with SASĀ® 9.2, these user-written functions can be called from a DATA step as well.PROC FCMP provides the ability to write true functions and CALL routines in DATA step syntax that are stored in a data set. In SAS 9.2, FCMP routines can be called from the DATA step like any other SAS function. This enables programmers to more easily read, write, and maintain complex code with independent and reusable subroutines.
SAS 9.2 Proc FCMP Documentation page – http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002975331.htm
Here are some examples of How to Use them…
NOTE: These Examples below work with SAS v9.2 ONLY.
Using Numeric Data in the FUNCTION Statement
The following example uses numeric data as input to the FUNCTION statement of PROC FCMP:
Using Numeric Data in the FUNCTION Statement
The following example uses numeric data as input to the FUNCTION statement of PROC FCMP:
proc fcmp; function inverse(in); if in=0 then inv=.; else inv=1/in; return(inv); endsub; run; data invert; input x; y=inverse(x); cards; 10 20 30 ; run;
Using Character Data in the FUNCTION Statement
The following example uses character data as input to the FUNCTION statement of PROC FCMP. The output from FUNCTION test is assigned a length of 12 bytes.
proc fcmp outlib=work.funcs.math; function test(x $) $ 12; if x = 'yes' then return('si si si'); else return('no'); endsub; run; data _null_; spanish=test('yes'); put spanish=; run;
Using Variable Arguments with an Array
The following example shows an array that accepts variable arguments. The example implies that the summation function can be called as follows: sum = summation(1, 2, 3, 4, 5); .
proc fcmp outlib=work.funcs.math; function summation (b[*]) varargs; total = 0; do i = 1 to dim(b); total = total + b[i]; end; return(total); endsub; data summate; array x(*) x1-x3 {10,20,30}; summ=summation(x); run;
Directory Traversal(v 9.2 ONLY)
Refer to this Paper for Detailed Explaination of a sample code
Example Codes
http://www.repole.com/dinosaur/age.html
http://www.sesug.org/SESUGOrganization/newsletters/fall2008/PROC_FCMP_200809.pdf#navpanes=0
http://www.sas.com/offices/asiapacific/sp/usergroups/snug/archive/2008/presentations/MichaelMatthewsQ408.pdf#navpanes=0