BookmarkSubscribeRSS Feed
Ken_oy
Fluorite | Level 6
How to make a %macro return a value like the "log()" does?

I want to use %macro in a data step, for example:

data test1;
set test2;
value1=%my_macro(value_in_test2);
run;

but don't know how...
:)
Appreciate for your advice!
8 REPLIES 8
Ken_oy
Fluorite | Level 6
Anyone can give me an example?
RickM
Fluorite | Level 6
It depends on what the macro does. It might be better to use proc fcmp to create a user defined function.
Peter_C
Rhodochrosite | Level 12
a macro can "do things" and/or return text.
It does "all that" at data-step compile-time.
So a %macro can't provide a log() of a run-time value, like a data set variable or DSDV(data step data vector) variable value.

So what did you want a macro to do?

can YOU give us an example, please?

peterC
Ken_oy
Fluorite | Level 6
yes. I have a dataset "one" containing all parameter information. I have a %macro step, which i can use to calculate the area under the Sin/Cos curve for each order. I have to check my output statement to see those values of AUC. I hope to get a dataset as "final":


***********************************************************;

my dataset "one" is:

a1 a2 b1 b2 order

1 2 3 4 1
2 3 33 4 2
12 2 3 1 3
...
............ 546

***********************************************************;
%macro AUC_hyper_(a,b,n,hBound, order );

data f1;
set one;
where order=$order;
w=1;

do i = 0 to (&n-1) ;
func=a1*cos((i+0.5)*w) + b1*sin((i+0.5)*w) + a2*cos((i+0.5)*w) + b2*sin((i+0.5)*w);

do;
y = func-&hBound;
output; end;

end;
run;

proc summary data=f1;
where y >0;
var y;
output out=p1 sum=areau;
run;
proc print data=p1;run;
%mend;

***********************************************************;
dataset "final";

a1 a2 b1 b2 order AUC

1 2 3 4 1 100
2 3 33 4 2 120
12 2 3 1 3 132
... ...
............ 546 140





***********************************************************;

Thanks!!
Cynthia_sas
SAS Super FREQ
Hi:
Well, I have a disconnect between the example you originally posted and the macro code that you posted just now. In the original example, your code implied that the macro program was returning 1 value:
[pre]
data test1;
set test2;
value1=%my_macro(value_in_test2);
run;
[/pre]

Which would argue for PROC FCMP and a user-defined function definition.

But in the macro you just showed (%AUC_hyper_), the macro program has a DATA step, a PROC SUMMARY step which creates WORK.P1 and then a PROC PRINT on dataset P1). Where did you envision the variable VALUE1 would get a value FROM???? From WORK.P1??? From WORK.F1???

What is $ORDER??? Where do the values for &N and &HBOUND come from???

Conceptually, you would be asking the macro facility to do something like this:
[pre]
data test1;
set test2;
value1 = insert ALL the code from
%AUC_hyper_(a,b,n,hBound, order ) macro program

;
run;
[/pre]

...which just will NOT work. Now, if you want to iterate through every observation in SET WORK.TEST2 and invoke the %AUC_hyper_ for every observation in WORK.TEST2, then you will want to read about the CALL EXECUTE method of placing macro code in the program stack and you will REALLY have to understand the timing issues of using CALL EXECUTE (such as the CALL EXECUTE will execute AFTER the DATA TEST1 step executes).

However, before you go down the CALL EXECUTE road, you might want to read and then search for more papers like these about calculating the area under the curve. A cursory review of some of these papers reveals that many folks have used SAS macros for performing this AUC task:
http://www2.sas.com/proceedings/sugi27/p229-27.pdf
http://www2.sas.com/proceedings/sugi27/p226-27.pdf
http://www.lexjansen.com/wuss/2004/posters/c_post_the_sas_calculations_.pdf
http://statcompute.spaces.live.com/blog/cns!39C8032DBD1321B7!583.entry
http://sphhp.buffalo.edu/biostat/research/techreports/UB_Biostatistics_TR0903.pdf
http://www.nesug.org/proceedings/nesug06/an/da29.pdf
http://www2.sas.com/proceedings/sugi28/275-28.pdf
http://www2.sas.com/proceedings/forum2008/231-2008.pdf

and at support.sas.com, these notes:
http://support.sas.com/kb/25/018.html
http://support.sas.com/kb/25/017.html
and the last note says:

NOTE: Beginning in SAS 9.2, comparison of areas under correlated ROC curves can be done using the ROC and ROCCONTRAST statements in PROC LOGISTIC. See the PROC LOGISTIC documentation for details and examples.
{endquote}


Perhaps some of the above will get you started on your task.

cynthia
Cynthia_sas
SAS Super FREQ
If you have an urgent need for help, you might wish to open a track with SAS Technical Support. To open a track with Tech Support, fill out the form at this link:
http://support.sas.com/ctx/supportform/createForm

cynthia
Ken_oy
Fluorite | Level 6
Thanks, reading all the links you mentioned 🙂
Cynthia_sas
SAS Super FREQ
Hi:
Remember that the macro program will get invoked at program COMPILE time and will result in resolved code being written to the DATA step program. The SAS Macro facility is very useful for text substitution and for generating repetitive statements, but what you envision doing requires an understanding of the difference between macro compile time, macro execution time, data step compile time and data step execution time and what happens during each phase of the process. So, in order to help with SAS macro questions it would be essential to actually see the full text of the %my_macro definition and for you to explain what the macro is supposed to do. (Also, since the question does involve SAS macro processing --and not SAS Procedures -- you might want to repost this question with more information in the SAS Macro forum).

If you want to make a macro that operates like a SAS function, then why not just make a user-defined functin with PROC FCMP???? These papers and articles and documentation might get you started -- depending on what you envision for the processing to take place.
http://www.repole.com/dinosaur/age.html
http://www.sesug.org/SESUGOrganization/newsletters/fall2008/PROC_FCMP_200809.pdf
http://support.sas.com/documentation/onlinedoc/base/91/fcmp.pdf
http://www.mwsug.org/proceedings/2009/appdev/MWSUG-2009-A03.pdf
http://support.sas.com/resources/papers/proceedings10/024-2010.pdf
http://support.sas.com/rnd/base/datastep/user-written-functions.pdf

cynthia

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4739 views
  • 0 likes
  • 4 in conversation