MetallicPriest
MetallicPriest

Reputation: 30765

How do you use user defined functions in SAS

I am new to SAS. I was trying to use a custom function in SAS, but it doesn't work. I tried the following.

....
proc fcmp outlib=work.f.f;
    function FtoC(temp);              
        Celsius = (5/9*(temp-32));     
        return(Celsius);               
    end func; /* This is line 22 */
run;
quit;

options cmplib=work.f;
DATA WORK.IMPORT;
    set WORK.IMPORT;
    Celsius = FtoC(temp);   
run;
....

But got the following error. What mistake am I doing?

ERROR 22-322: Expecting ;.  
ERROR 202-322: The option or parameter is not recognized and will be ignored.
ERROR 68-185: The function FTOC is unknown, or cannot be accessed.
ERROR: Variable CELSIUS not found.
ERROR: Variable Celsius is not on file WORK.IMPORT.

Upvotes: 0

Views: 878

Answers (2)

Tom
Tom

Reputation: 51566

Notice that the full SAS log will add a lot more context to the error messages.

1     proc fcmp outlib=work.f.f;
2         function FtoC(temp);
3             Celsius = (5/9*(temp-32));
4             return(Celsius);
5         end func; /* This is line 22 */
              ----
              22
              202
ERROR 22-322: Expecting ;.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
6     run;

NOTE: Execution aborted because of errors in program.
      Reenter the corrected program or enter "QUIT;" to exit procedure.
7     quit;

Fix the first error by using ENDSUB to end the function definition.

15    proc fcmp outlib=work.f.f;
16        function FtoC(temp);
17            Celsius = (5/9*(temp-32));
18            return(Celsius);
19        endsub;
20    run;

NOTE: Function FtoC saved to work.f.f.
NOTE: PROCEDURE FCMP used (Total process time):
      real time           0.07 seconds
      cpu time            0.06 seconds

This will make the second error go away.

Upvotes: 5

MetallicPriest
MetallicPriest

Reputation: 30765

The error was that I was using end func instead of endsub.

Upvotes: 0

Related Questions