FCMP Procedure

Example 7: Using GTL with User-Defined Functions

Features:
PROC FCMP functions:
OSCILLATE
OSCILLATEBOUND
Other procedures:
PROC TEMPLATE
PROC SGRENDER
The following example shows how to use functions in a GTL EVAL function.

Details

The following example shows how to define functions that define new curve types (oscillate and oscillateBound). These functions can be used in a GTL EVAL function to compute new columns that are presented with a seriesplot and bandplot.

Program

proc fcmp outlib=sasuser.funcs.curves;
   function oscillate(x,amplitude,frequency);
         if amplitude le 0 then amp=1; else amp=amplitude;
	      if frequency le 0 then freq=1; else freq=frequency;
         y=sin(freq*x)*constant("e")**(-amp*x);
         return (y);
  endsub;
 function oscillateBound(x,amplitude);
       if amplitude le 0 then amp=1; else amp=amplitude;
       y=constant("e")**(-amp*x);
       return (y);
  endsub;
 run;
 options cmplib=sasuser.funcs;

data range;
   do Time=0 to 2 by .01;
   output;
   end;
run;
proc template ;
   define statgraph damping;
   dynamic X AMP FREQ;
   begingraph;
       entrytitle "Damped Harmonic Oscillation";
       layout overlay / yaxisopts=(label="Displacement");
          if (exists(X) and exists(AMP) and exists(FREQ))
	            bandplot x=X  limitlower=eval(-oscillateBound(X,AMP))
                limitupper=eval(oscillateBound(X,AMP));
                seriesplot x=X y=eval(oscillate(X,AMP,FREQ));
	      endif;
	    endlayout;
   endgraph;
   end;
   run;
 proc sgrender data=range template=damping;
      dynamic x="Time" amp=10 freq=50 ;
 run;

Program Description

Create the OSCILLATE function.
proc fcmp outlib=sasuser.funcs.curves;
   function oscillate(x,amplitude,frequency);
         if amplitude le 0 then amp=1; else amp=amplitude;
	      if frequency le 0 then freq=1; else freq=frequency;
         y=sin(freq*x)*constant("e")**(-amp*x);
         return (y);
  endsub;
Create the OSCILLATEBOUND function.
 function oscillateBound(x,amplitude);
       if amplitude le 0 then amp=1; else amp=amplitude;
       y=constant("e")**(-amp*x);
       return (y);
  endsub;
 run;
Create a data set called RANGE that will be used by PROC SGRENDER.
 options cmplib=sasuser.funcs;

data range;
   do Time=0 to 2 by .01;
   output;
   end;
run;
Use the TEMPLATE procedure to customize the appearance of your SAS output.
proc template ;
   define statgraph damping;
   dynamic X AMP FREQ;
   begingraph;
       entrytitle "Damped Harmonic Oscillation";
       layout overlay / yaxisopts=(label="Displacement");
          if (exists(X) and exists(AMP) and exists(FREQ))
	            bandplot x=X  limitlower=eval(-oscillateBound(X,AMP))
                limitupper=eval(oscillateBound(X,AMP));
                seriesplot x=X y=eval(oscillate(X,AMP,FREQ));
	      endif;
	    endlayout;
   endgraph;
   end;
   run;
Use the SGRENDER procedure to identify the data set that contains the input variables and to assign a statgraph template for the output.
 proc sgrender data=range template=damping;
      dynamic x="Time" amp=10 freq=50 ;
 run;

Output: HTML

Output from Using GTL with User-Defined Functions
Output from Using GTL with User-Defined Functions