# Example 12: Creating a Function to Use as a Format

 Features: PROC FCMP statement CMPLIB= system option PROC FORMAT statement Function as a format feature

### Details

This example creates a function that converts temperatures from Celsius to Fahrenheit and Fahrenheit to Celsius. The program uses the function as a function one DATA step and then as a format in another DATA step.

## Program

```proc fcmp outlib=library.functions.smd;
function ctof(c) \$;
return(cats(((9*c)/5)+32,'F'));
endsub;

function ftoc(f) \$;
return(cats((f-32)*5/9,'C'));
endsub;

run;```
`options cmplib=(library.functions);`
```data _null_;
f=ctof(100);
put f=;
run;```
```proc format;
value ctof (default=10) other=[ctof()];
value ftoc (default=10) other=[ftoc()];
run;
```
```data _null_;
c=100;
put c=ctof.;
f=212;
put f=ftoc.;
run;```

## Program Description

Create the functions that change temperature from Celsius to Fahrenheit and Fahrenheit to Celsius.The FCMP procedure creates the CTOF function to convert Celsius temperatures to Fahrenheit and the FTOC to convert Fahrenheit temperatures to Celsius.
```proc fcmp outlib=library.functions.smd;
function ctof(c) \$;
return(cats(((9*c)/5)+32,'F'));
endsub;

function ftoc(f) \$;
return(cats((f-32)*5/9,'C'));
endsub;

run;```
Access the function library.The CMPLIB system option enables the functions to be included during program compilation.
`options cmplib=(library.functions);`
Use the function as a function in a SAS program.
```data _null_;
f=ctof(100);
put f=;
run;```
Create user-defined formats using the functions.The name of the format is the name of the function. When you use a function as a format, you can nest the format as shown by the OTHER keyword.
```proc format;
value ctof (default=10) other=[ctof()];
value ftoc (default=10) other=[ftoc()];
run;
```
Use the function as a format.This DATA step formats temperatures using a named PUT statement, where you assign a format to a variable in the PUT statement.
```data _null_;
c=100;
put c=ctof.;
f=212;
put f=ftoc.;
run;```

## Output: Log

```323  proc fcmp outlib=library.functions.smd;
324     function ctof(c) \$;
325        return(cats(((9*c)/5)+32,'F'));
326     endsub;
327
328     function ftoc(f) \$;
329        return(cats((f-32)*5/9,'C'));
330     endsub;
331
332  run;

NOTE: Function ftoc saved to library.functions.smd.
NOTE: Function ctof saved to library.functions.smd.
NOTE: PROCEDURE FCMP used (Total process time):
real time           17.59 seconds
cpu time            1.26 seconds

333
334  options cmplib=(library.functions);
335
336  data _null_;
337     f=ctof(100);
338     put f=;
339  run;

f=212F
NOTE: DATA statement used (Total process time):
real time           0.50 seconds
cpu time            0.01 seconds

340
341  proc format;
342       value ctof (default=10) other=[ctof()];
NOTE: Format CTOF has been output.
343       value ftoc (default=10) other=[ftoc()];
NOTE: Format FTOC has been output.
344       run;

NOTE: PROCEDURE FORMAT used (Total process time):
real time           0.00 seconds
cpu time            0.00 seconds

345
346  data _null_;
347     c=100;
348     put c=ctof.;
349     f=212;
350     put f=ftoc.;
351  run;

c=212F
f=100C
```