How to Create a Function in SAS

Reply
Occasional Contributor
Posts: 9

How to Create a Function in SAS

I'm trying to create a very simple function in SAS 9.3 where I send an address value to a custom-built function, and I want to return an updated value.  Here is where the function is created:

proc fcmp outlib=work.functions.test;  

function Clean(address);

CleanAddress=compress(address,".");

return (CleanAddress);

endsub;

I'm not certain what to put for the oulib in the proc fcmp line.  Will it work if I use "work.functions.test"?

So basically I want to send the address to the Clean() function and use compress to remove any periods.  Here is where I call the function:

options complib=work.functions;

data _null_;

NewAddress=Clean("123 Jefferson Ave.");

put NewAdress;

run;

I tried many many variations of this, and I keep getting the error saying unable to convet a character value to numeric.  Any suggestions on what I'm doing wrong?  The error log is below. Your help is greatly appreciated!

ERROR: Unable to convert a character value to a numeric value in function 'Clean' in statement number 3 at line 6 column 2.

       The statement was:

    0      (6:2)     _Clean_ = Move N<=C(CleanAddress="            ")

ERROR: Exception occurred during subroutine call.

.

NewAddress=. NewAdress=. _ERROR_=1 _N_=1

Occasional Contributor
Posts: 9

Re: How to Create a Function in SAS

It looks like I need to let the function know that the input is character type.

function Clean(address $) $ 50;

CleanAddress=compress(address,".");

return (CleanAddress);

endsub;

Ask a Question
Discussion stats
  • 1 reply
  • 430 views
  • 1 like
  • 1 in conversation