BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mazouz
Calcite | Level 5

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

In a DATA step, this is how to do it.

data nothing;
	do i= 1, 3, 5, 7, 20,100,2000;
	    output;
	end;
run;

 

 

--
Paige Miller
Tom
Super User Tom
Super User

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;

 

mazouz
Calcite | Level 5

 

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

 

Tom
Super User Tom
Super User

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?

mazouz
Calcite | Level 5

Yes

%FUNCTION is a macro that i want to iterat for i in list 

Tom
Super User Tom
Super User

@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)
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
mazouz
Calcite | Level 5
thank you 😉
mazouz
Calcite | Level 5

this time i want to iterate over a list of char variable. can I use same code?

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Kurt_Bremser
Super User

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 11 replies
  • 3528 views
  • 2 likes
  • 4 in conversation