Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions in SAS

Tags:

python

sas

I have a function that converts a value in one format to another. It's analogous to converting Fahrenheit to Celsius for example.

Quite simply, the formula is:

l = -log(20/x)

I am inheriting SAS code from a colleague that has the following hardcoded for a range of values of x:

"if x= 'a' then x=l;"

which is obviously tedious and limited in scope.

How best could I convert this to a function that could be called in a SAS script?

I previously had it in Python as:

def function(x):
    l = -np.log10(20/float(x))
    return l

and then would simply call the function.

Thank you for your help - I'm adusting from Python to SAS and trying to figure out how to make the switch.

like image 526
2567655222 Avatar asked Oct 28 '25 09:10

2567655222


2 Answers

If you are interested in writing your own functions, as Joe said, proc fcmp is one way to do it. This will let you create functions that behave like SAS functions. It's about as analogous to Python functions as you'll get.

It takes a small bit of setup, but it's really nice in that the functions are all saved in a SAS dataset that can be transferred from environment to environment.

The below code creates a function called f() that does the same as the Python function.

proc fcmp outlib=work.funcs.log;
    function f(x);
        l = log10(20/x);
        return(l);
    endfunc;
run;

options cmplib=work.funcs;

This is doing three things:

  1. Creating a function called f() that takes one input, x
  2. Saving the function in a dataset called work.funcs that holds all functions
  3. Labeling all the functions under the package log

Don't worry too much about the label. It's handy if you have many different function packages you want, for example: time, dates, strings, etc. It's helpful for organization, but it is a required label. Most of the time I just do work.funcs.funcs.

options cmplib=work.funcs says to load the dataset funcs which holds all of your functions of interest.

You can test your function below:

data test;
    l1  = f(1);
    l2  = f(2);
    l10 = f(10);
run;

Output:

l1              l2  l10
1.3010299957    1   0.3010299957

Also, SAS does have a Python interface. If you're more comfortable programming in Python, take a look at SASPy to get all the benefits of both SAS and Python.

like image 191
Stu Sztukowski Avatar answered Oct 31 '25 10:10

Stu Sztukowski


The typical way you'd do this is in a SAS Macro.

    %macro func(x);
      -log10(20/&x)
    %mend func;

data whatever;
  set yourdataset;
  l = %func(x);
run;

You can also of course just directly use it in code if it's trivial like this.

data whatever;
  set yourdataset;
  l = -log10(20/x);
run;

There are actual functions in SAS, but they're not really used very commonly. FCMP is the procedure where you construct those. Unfortunately, they're less efficient than macros or directly writing the code.

like image 32
Joe Avatar answered Oct 31 '25 11:10

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!