BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
8 REPLIES 8
Nancy_B
Calcite | Level 5
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);
run;

Hope this helps,
Nancy
deleted_user
Not applicable
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?
deleted_user
Not applicable
Hi,

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
Rao

data trading_date;
input tra_date $;
datalines;
20030104
20030105
20030106
20030109
20030110
;
run;
%macro checkbeforetd(chdate);
%let chdate=&chdate;
data trading_date(drop=tra_date);
format trad_date ddmmyyp10.;
length trad_date 5.;
set trading_date;
trad_date=input(put(tra_date,8.),yymmdd8.);
put trad_date;
run;
proc sql noprint;
select max(trad_date) format ddmmyyp10. into :reqoutput from trading_date
where trad_date lt &chdate;
quit;
%put &reqoutput;
%mend;
%checkbeforetd('06Jan2003'd);
deleted_user
Not applicable
But this can not use like a function in SAS.
deleted_user
Not applicable
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 http://support.sas.com/documentation/onlinedoc/base/91/proto.pdf 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.
TobyDunn_hotmail_com
Fluorite | Level 6
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.
naag
Calcite | Level 5
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.


-----------------------------------------------

Dx
----------------------------------------------
Hotmail Com-Hotmail Com
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 8 replies
  • 1454 views
  • 0 likes
  • 5 in conversation