Hello,
I want to creat a loop that that iterate over a list of numeric value like this :
LIST => [1, 3, 5, 7, 20,100,2000] %DO i %IN LIST;
I don't know what %FUNCTION is, I don't have that macro.
However, the rest of your code works fine if I comment out %FUNCTION
%let list = 1 3 5 7 20 100 2000;
%MACRO WANT(variable,table);
%do index = 1 %to %sysfunc(countw(&list,%str( )));
%let I =%scan(&list,&index,%str( ));
/*%FUNCTION(&variable,&table,I);*/
%put &=i;
%END;
%MEND;
%WANT(VAR1,HAVE)
Perhaps you should use
%FUNCTION(&variable,&table,&I);
Important concept for your future reference: saying something "don't work" and providing no other information is never helpful to us and is never helpful to you in resolving these issues.
In a DATA step, this is how to do it.
data nothing;
do i= 1, 3, 5, 7, 20,100,2000;
output;
end;
run;
Do you need to do this in real SAS code? Your syntax looks like IML are you using IML? The data step also can use a comma separated list of values in a DO statement.
do i= 1, 3, 5, 7, 20,100,2000;
Some of them can be ranges if you want.
do i= 1 to 7 by 2, 20,100,2000;
Or are you trying to use the macro processor to generate something? The macro processor does not have any syntax for that. Just make a list and iterate over an index into the list.
%let list = 1 3 5 7 20 100 2000;
%do index = 1 %to %sysfunc(countw(&list,%str( )));
%let I =%scan(&list,&index,%str( ));
....
%end;
%let list = 1 3 5 7 20 100 2000;
%MACRO WANT(variable,table); %do index = 1 %to %sysfunc(countw(&list,%str( ))); %let I =%scan(&list,&index,%str( )); %FUNCTION(&variable,&table,I); %END; %MEND; %WANT(VAR1,HAVE);
I want to iterate function, I try this but don't work.
Define how it doesn't work. I notice that you are setting a value to I but you never use it. Did you mean to use &I instead of I in the call to %FUNCTION() macro?
Yes
%FUNCTION is a macro that i want to iterat for i in list
@mazouz wrote:
Yes
%FUNCTION is a macro that i want to iterat for i in list
Does that macro want a number as the value of the third parameter or a macro variable name? Currently you are just giving it the letter I.
%function(...,i)
If you want to give it the values like 1 and 2000 instead then use &I in the call to the macro.
%function(....,&i)
I don't know what %FUNCTION is, I don't have that macro.
However, the rest of your code works fine if I comment out %FUNCTION
%let list = 1 3 5 7 20 100 2000;
%MACRO WANT(variable,table);
%do index = 1 %to %sysfunc(countw(&list,%str( )));
%let I =%scan(&list,&index,%str( ));
/*%FUNCTION(&variable,&table,I);*/
%put &=i;
%END;
%MEND;
%WANT(VAR1,HAVE)
Perhaps you should use
%FUNCTION(&variable,&table,&I);
Important concept for your future reference: saying something "don't work" and providing no other information is never helpful to us and is never helpful to you in resolving these issues.
this time i want to iterate over a list of char variable. can I use same code?
@mazouz wrote:
this time i want to iterate over a list of char variable. can I use same code?
Depending on how your macro %FUNCTION is written, the answer is either YES or NO.
But the macro loop that we are working with doesn't care if the values are numeric or character.
Here
%FUNCTION(&variable,&table,I);
you feed the one-character text I to your macro. If you wanted it to get the value stored in the macro variable I, you need to address it correctly:
%FUNCTION(&variable,&table,&I);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.