can i write my own function in sas?

Posts: 0

can i write my own function in sas?

In SAS,if I having a value to return to orther use, I only know that we can use macro variable. Can I use sas language to write my owner function with return value in SAS? Or write my owner macro function with return value in SAS?
New Contributor
Posts: 3

Re: can i write my own function in sas?

Hi Bill,

Yes, you can write macros which operate as functions. For example, suppose you need to compute the study day of a visit, and you have both the visit date and first day of study med (Day 1) on the record. The following macro can be called as a function:

%macro studyday (visdt, day1dt);

visdt - day1dt + (visdt >= day1dt);

%mend studyday;

It could be invoked in a DATA step like this:

data studydys;
set olddata;

studyday = %studyday(visdt, day1dt);

Hope this helps,
Posts: 0

Re: can i write my own function in sas?

Hi Nancy, thanks for your answer. This method sometime may be usefull, but I have a question, can't use this method.
The problem is that: I have a table TRADING_DATE whith variable t_date means all the trading date, with the given date, I must give the last trading date before the given date.
For example, the table have the record, 20030104,20030105,200306,20030109,20030110,....., with the given date 20030108, the last trading date before is 20030106.
Can this writen as a function in SAS?
Posts: 0

Re: can i write my own function in sas?


Can you pls try the following code, may be this helps, if not pls let me know.. Check in your log file to the output. You can as well use the derived variable if required.

Best Cheers

data trading_date;
input tra_date $;
%macro checkbeforetd(chdate);
%let chdate=&chdate;
data trading_date(drop=tra_date);
format trad_date ddmmyyp10.;
length trad_date 5.;
set trading_date;
put trad_date;
proc sql noprint;
select max(trad_date) format ddmmyyp10. into :reqoutput from trading_date
where trad_date lt &chdate;
%put &reqoutput;
Posts: 0

Re: can i write my own function in sas?

But this can not use like a function in SAS.
Posts: 0

Re: can i write my own function in sas?

Writing your own function in SAS is problematic. You might consider writing functions in a language supported by SAS and registering them with SAS. See for more information.

You will need to write your functions in C or C++. If you know the basics of either language, that's all you need for simple tasks.
Occasional Contributor
Posts: 10

Re: can i write my own function in sas?

Bill ,

Technically you cant really do this until Version 9.2 comes out.

Now lets consider some non normal ways of getting what you want:

Data One ;
X = 10 ;
Output ;

X = 20 ;
Output ;

Run ;

Options Mprint Mlogic Symbolgen ;
%Macro Mult( Var = , Mlp = ) ;
%SysEvalF( &Var * &Mlp )
%Mend Mult ;

Data Need ;
Set One ;
MyNum = Resolve( '%Mult( Var = ' || Put( X , 8. -L ) || ', MLP = 10 )' ) ;

Put X= MyNum= ;
Run ;

Here I use the resolve function to delay the execution of the macro %Mult until the data steps execution time. This allows one to pass variable X's values to the macro with each observation. The Data Step releases control to the macro facility where it does the math and the macro facility passes back the new value and releases control back to the data step.
Posts: 1

Re: can i write my own function in sas?

Many of the features in SAS and S that I like or dislike are similar to
the last paragraph, in that they are consequences of the way in which the
underlying package is organized. They are not right or wrong, nor can they
be `fixed', but in many particular instances one or the other can be
very inconvenient. Though GUI interfaces are a good thing in their own
right, and are a big help to a certain class of user, they cannot undo some
of these features: PROC INSIGHT is not, nor is it even close to, a
replacement for S graphics.


Hotmail Com-Hotmail Com
Super Contributor
Super Contributor
Posts: 3,174

Re: can i write my own function in sas?

For consideration, I was able to execute the SAS RESOLVE function in SAS 9.1.3 SP4, using the sample code provided by TobyDunn. I remember using this function in the past but never for numeric data calculations though. Thanks to Toby for the extra idea.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 8 replies
  • 5 in conversation