SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Do loop

Reply
Frequent Contributor
Posts: 84

Do loop

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;

Super User
Posts: 5,254

Re: Do loop

Try %eval ()

Data never sleeps
Frequent Contributor
Posts: 84

Re: Do loop

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

Super User
Super User
Posts: 7,392

Re: Do loop

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;

Frequent Contributor
Posts: 84

Re: Do loop

%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

Super User
Posts: 6,927

Re: Do loop

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 84

Re: Do loop

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

Super User
Posts: 6,927

Re: Do loop

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,927

Re: Do loop

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 2,174

Re: Do loop

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 

Super User
Super User
Posts: 7,392

Re: Do loop

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

Ask a Question
Discussion stats
  • 10 replies
  • 562 views
  • 2 likes
  • 5 in conversation