I have written following sas code it works fine
DATA multiply;
answer = 0;
do i = 1 to 4;
answer=answer + 3;
output;
end;
RUN;
Proc print data= multiply;
run;
Output is
Obs answer i
1 3 1
2 6 2
3 9 3
4 12 4
But when i try to do this through macro i didn't work correctly I am using this macro code
%macro Power;
%let answer=0;
%do i = 1 %to 5;
%let answer=&answer+3;
output;
%end;
%mend;
it is not working and hence did not get same out as above
Kindly help me in this regard
%Power;
Try %eval ()
Thanks for ur reply
could u mention where I have to use this %eval() in above code
or could u write this completed short macro code
Regards
Its when you try to evaluate the macro variable + something:
%macro Power;
%let answer=0;
%do i = 1 %to 5;
%let answer=%eval(&answer+3);
output;
%end;
%mend;
However, why do you need to do it this why anyways when base SAS can do it?
data _null_;
answer = 0;
do i=1 to 4;
answer=answer + 3;
put answer;
end;
run;
%Power is not yielding the output but producing errors in log window like
Line generated by the invoked macro "POWER".
output;
------
How can i get the output plz guide
Read my post, the answer is there. And thoroughly study this:
SAS(R) 9.4 Macro Language: Reference, Third Edition
since it seems that you have an incorrect understanding of what the macro language does.
i did this
%macro Power;
Data power1;
count=0;
%do i = 1 %to 5;
count=count+1;
output;
%end;
run;
Proc print data=power1;
run;
%mend;
%Power;
It is working fine
It is still an abuse of the macro engine.
data power1;
do count = 1 to 5;
output;
end;
run;
does the same
The macro creates the following code:
data power1;
count = 0;
count=count+1;
output;
count=count+1;
output;
count=count+1;
output;
count=count+1;
output;
count=count+1;
output;
run;
Crushing peanuts with an elephant's hoof.
%Power; on its own will only get you a syntax error, unless you wrapped it into a data step, where it will output meaningless records.
The macro itself yields only this:
output;
output;
output;
output;
output;
The macro engine is a text generator that creates program text BEFORE(!!!!) any action is taken.
It is there to dynamically generate program text if the need for this arises. This is not the case in your example AT ALL.
Macro variables are only "visible" to the macro engine; only when they are placed outside of macro statements, their contents are then fed to the main SAS interpreter as program text(!) by the macro engine.
Rasheed
I can see you are flexing muscles as you learn the functionality of SAS
best of luck
peterC
ps
the manuals are online and worth reading for all the examples
Problem is, in the example given he isn't learning the functionality of SAS, just learning how to obfuscate his code. It would be far better if learning macro programming to firstly read the documentation to understand what macro is, then writing something for a specific purpose which isn't directly covered by Base SAS. Too often seems to be the case that lack of Base SAS is covered over by illogical use of macro language. I believe this is what the term "Crushing peanuts with an elephant's hoof." refers to, although I think elephants actually eat the nuts with the shells on...
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.