BookmarkSubscribeRSS Feed
samira
Obsidian | Level 7

 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.
 
Thanks for any help;
Samira
5 REPLIES 5
collinelliot
Barite | Level 11

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.

samira
Obsidian | Level 7

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!

ballardw
Super User

@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;

Tom
Super User Tom
Super User

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.

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 Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 516 views
  • 1 like
  • 5 in conversation