DECLARE PACKAGE Statement: FCMP Package
Creates a package variable and gives you the option to create an instance of the FCMP package.
| Category: | Local |
|---|---|
| Restriction: | This statement is not supported on the CAS server. |
| Requirement: | The PACKAGE statement is required before you use the DECLARE PACKAGE statement. |
| Note: | DS2 supports PROC FCMP OUTARGS statement arguments with code blocks from the PROC FCMP SUBROUTINE statement. DS2 does not support OUTARGS statement arguments with code blocks from the PROC FCMP FUNCTION statement. |
Table of Contents
Syntax
Required Arguments
fcmp-package-name
specifies the name of the FCMP package.
| Requirement | The package name must match the name of a package created in a PACKAGE statement, or an error will occur. |
|---|---|
| See | PACKAGE Statement |
variable
specifies a name that can reference an instance of the package.
Details
A DS2 package is a collection of variables and methods of which particular instances can be constructed and used in other DS2 programs.
You use an FCMP package to support calls to functions and subroutines that are available or are created with the FCMP procedure. The FCMP package is predefined for DS2 programs.
When a package is declared, a variable is created that can reference an instance of the package. If constructor arguments are provided with the package variable declaration, then a package instance is constructed and the package variable is set to reference the constructed package instance. Multiple package variables can be created and multiple package instances can be constructed with a single DECLARE PACKAGE statement, and each package instance represents a completely separate copy of the package.
You declare an FCMP package by using the DECLARE PACKAGE statement. This associates an FCMP package with an FCMP name. After you declare the new FCMP package, you can call the functions and subroutines that are created with the FCMP procedure.
There are two ways to construct an instance of an FCMP package.
- Use the DECLARE PACKAGE
statement along with the_NEW_ operator:
declare package fcmp pharma; pharma = _new_ fcmp(); - Use the DECLARE PACKAGE
statement along with its constructor syntax:
declare package fcmp pharma();
For more information about FCMP packages, see Using the FCMP Package in SAS DS2 Programmer’s Guide.
Examples
Example 1: Using FCMP OUTARGS Parameters
The following example
creates FCMP subroutine named package1 that
uses OUTARGS parameters. The FCMP procedure’s OUTARGS parameter
is treated as an IN_OUT parameter in the METHOD statement.
libname base '.';
proc fcmp outlib = base.fcmpsubs.package1;
subroutine swapper(a,b);
outargs a,b;
t1 = b; b = a; a = t1;
endsub;
run;
quit;
proc ds2;
package pkg / overwrite=yes language='fcmp' table='base.fcmpsubs';
run;
data _null_;
dcl package pkg p();
method init();
dcl double x y;
x=10;
y=42;
put 'before:' x= y=;
p.swapper(x,y);
put 'after:' x= y=;
end;
enddata;
run;
quit;
The following lines are written to the SAS log.
before: x=10 y=42 after: x=42 y=10
Example 2: FCMP Package Using DOUBLE Arguments
This example walks through creation of a square routine in FCMP and using that routine from a DS2 program. The current directory is used as the "library" of FCMP packages.
libname base '.';
* fcmp defines a function, square;
proc fcmp outlib = base.fcmpsubs.package1;
function square(a);
return (a*a);
endsub;
run;
quit;
* define the ds2 package thru which the fcmp functions will be called;
proc ds2;
package pkg /overwrite=yes language='fcmp' table='base.fcmpsubs';
run;
* demonstration of calling fcmp thru the ds2 wrapper package;
data _null_;
dcl package pkg p();
dcl double a b;
method init();
do a = 10 to 20;
b=p.square(a);
put a= b=;
end;
end;
enddata;
run;
quit;
The following lines are written to the SAS log.
a=10 b=100 a=11 b=121 a=12 b=144 a=13 b=169 a=14 b=196 a=15 b=225 a=16 b=256 a=17 b=289 a=18 b=324 a=19 b=361 a=20 b=400
Example 3: FCMP Package with Character Arguments
libname base '.';
proc fcmp outlib = base.fcmpsubs.package1;
function f(a $) $ 10;
return (trim(a) !! trim(a));
endsub;
run;
proc ds2;
package pkg /overwrite=yes language='fcmp' table='base.fcmpsubs';
run;
data _null_;
dcl package pkg p();
method runone(double arg, double expected);
dcl double actual;
actual=p.f(arg);
if (actual ~= expected) then put 'ERROR:' arg= expected= actual=;
end;
method init();
runone(5, 55);
runone(345, 345345);
runone(10, 1010);
runone(4.2, .); * can't convert back to double w/ two '.' chars;
runone(., .);
end;
enddata;
run;
quit;
See Also
-
Using the FCMP Package in SAS DS2 Programmer’s Guide
-
Package Constructors and Destructors in SAS DS2 Programmer’s Guide
-
Operators:
-
Statements: