BookmarkSubscribeRSS Feed
Rasheed
Calcite | Level 5

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;

10 REPLIES 10
LinusH
Tourmaline | Level 20

Try %eval ()

Data never sleeps
Rasheed
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Rasheed
Calcite | Level 5

%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

Rasheed
Calcite | Level 5

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

Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

%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.

Peter_C
Rhodochrosite | Level 12

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 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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...

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!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 10 replies
  • 1512 views
  • 2 likes
  • 5 in conversation