Hello,
I have started to figure out how can I use do loop in macro and I ran the below code (I found from this website). but I got error.
Here is the code:
%macro control (list=, start_date=, end_date=);
%local list;
%do i=1 %to %sysfunc(countw(&list));
%let key = %scan(&list,&i);
%let key2= %scan(&start_date,&i); /* you already had the pattern for this statement*/
%let key3= %scan(&start_date,&i);
%macro_call(&key,&key2,&key3);
%end;
%mend;
%control(list=1234 2456 3567 4567 5674 8765,
start_date= 03-SEP-16 10-SEP-16 17-SEP-16 24-SEP-16 01-OCT16,
end_date = 09-SEP-16 16-SEP-16 23-SEP-16 30-SEP-16 07-OCT-16 );
and I got following error:
114 %macro_call(&key,&key2,&key3);
_
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
MLOGIC(CONTROL): %DO loop index variable I is now 7; loop will not iterate again.
Is there actually a macro called "macro_call" defined and compiled somewhere?
I think the code you have is using it as an example of how to invoke one macro within another, but that macro has to exist.
no actually that is all I have in my program. I am very new in SAS and I was trying to figure out how can I use such loops. and I thought macro_call is a defult statement in SAS. isn't? If not how can I print out the variable within loop?
yes I found the code at : https://communities.sas.com/t5/Base-SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-f...
Thank you for the reply!
That post was written to illustrate calling a macro in a loop. macro_call is just a stand-in for any macro you need to execute repeatedly.
@samira wrote:
no actually that is all I have in my program. I am very new in SAS and I was trying to figure out how can I use such loops. and I thought macro_call is a defult statement in SAS. isn't? If not how can I print out the variable within loop?
yes I found the code at : https://communities.sas.com/t5/Base-SAS-Programming/How-can-access-a-macro-do-loop-or-do-loop-only-f...
Thank you for the reply!
To see the result of what you have done so far instead of the macro call use %put key=&key key2=&key2 key3=&key3;
Since you don't actually have another macro that you are calling (yet) then just put in a %PUT statement so you can see what vlaues you have assigned.
%put (&key1,&key2,&key3);
Also you do not need to add a %LOCAL statement for the macro parameters. They will already be local macro variables. Instead you should define the macro variables you are creating as local. Otherwise you might change the values of existing macro variables.
%local i key1 key2 key3;
Note you also probably want to define KEY3 to take the value from the list of end dates instead of getting another copy of the start date.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.