DATA Step, Macro, Functions and more

Calling a Macro from within a compute block in a proc report

Reply
Regular Contributor
Posts: 229

Calling a Macro from within a compute block in a proc report

Is it possible?

Do you have an example?
Super User
Posts: 9,676

Re: Calling a Macro from within a compute block in a proc report

It looks like can not do it.
Because SAS does not have proc's interface with macro facility.
But you can make a macro to generate the macro code.
Such as:
%macro report....
proc report..
compute..
%define_style...
........


Ksharp
Super Contributor
Posts: 394

Re: Calling a Macro from within a compute block in a proc report

Yes, it's possible to have a macro within a compute block. Remember that macros are expanded before your program runs. Therefore the statements in your macro are executed before PROC REPORT starts running.

"The text substitution produced by the macro processor is completed before the program text is compiled and executed. The macro facility uses statements and functions that resemble the statements and functions that you use in the DATA step. An important difference, however, is that macro language elements can enable only text substitution and are not present during program or command execution." -- http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#getstart.htm
SAS Super FREQ
Posts: 8,742

Re: Calling a Macro from within a compute block in a proc report

Hi:
Tim has pointed you to the documentation. That's a good reference. This is also a good paper on the basics of SAS Macro processing.
http://www2.sas.com/proceedings/sugi28/056-28.pdf

The program below has several examples of using the SAS Macro facility inside PROC REPORT. The phrase "calling a macro" generally implies that you are calling a macro program. The %GENCD macro program is being called or invoked several times within the PROC REPORT program. At compile, each of these invocations will be turned into a CALL DEFINE statement. Then, at execution time, the CALL DEFINE statement may or may not be executed, depending on the values being processed by each COMPUTE block.

The SAS Macro facility will only generate code. When the macro trigger of '%' is encountered, the macro facility word scanner will take over to resolve any macro triggers. So, by the time the word scanner is done, a syntactically correct CALL DEFINE statement will be generated inside the PROC REPORT code. When the PROC REPORT code is sent forward to the compiler, there will be NO macro triggers left in the code. In this way, the Macro facility is acting as a typewriter to type code for me. It would not be appropriate to have a macro program, inside PROC REPORT, generate a PROC MEANS, PROC SQL, etc step or a DATA step -- or try to perform any kind of processing that is not normally possible inside a COMPUTE block

Another form of macro trigger is the '&', which you can see in the macro variable reference &SPHD. As with the % trigger, the &SPHD will be resolved at compile time and the string 'Average' will be substituted for the macro variable &SPHD in the column statement.

cynthia
[pre]
%macro gencd(chgs=,color=);

%if &chgs = yes %then %do;
call define(_col_,'style',"style={background=&color}");
%end;
%else %if &chgs = url %then %do;
call define('name','style','style={url="http://www.sas.com"}');
%end;

%mend gencd;

%let sphd = Average;

options mlogic symbolgen mprint;
ods html file='c:\temp\usemacro.html' style=sasweb;
proc report data=sashelp.class nowd;
column sex name age ("&sphd" height weight);
define sex / order;
define name / order;
define age / display;
define height /mean;
define weight / mean;
rbreak after / summarize;
compute height;
if height.mean lt 60 then do;
%gencd(chgs=yes,color=cyan)
end;
endcomp;
compute weight;
if name = 'Barbara' then do;
%gencd(chgs=url)
%gencd(chgs=yes, color=cx6495ed)
end;
else do;
if weight.mean gt 100 and _break_ = ' ' then do;
%gencd(chgs=yes, color=yellow)
end;
end;
endcomp;
run;
ods _all_ close;
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 216 views
  • 0 likes
  • 4 in conversation