## Do loop error problem

Solved
Occasional Contributor
Posts: 13

# Do loop error problem

hello . I would like to generate several data set by using macro and do loop. but the log message keep alarming .

I don't know why eval is needed .

data work.aa;
input a ;
cards;
1
2
3
4
5
6
7
8
9
10

;
run;

%macro do_loop ;
%do i=1 %to 5 ;
%do j=i+1 to %to 7 ;

data date&i.&j. ;
set work.aa;

if 0<=a<&i then do ;
own1=1 ;
own2= 0 ;
end;
else if &i<= a =<&j then
do;
own1 = 0 ;
own2 = 0 ;
end;

else if &j < a <10 then do ;
own1 = 0 ;
own2 = 1 ;
end;
run;
proc print; run;

%end ;
%end;
%mend ;

%do_loop

Accepted Solutions
Solution
a month ago
Super User
Posts: 10,209

## Re: Do loop error problem

To use a macro variable, you always need the ampersand, and in macro %do statements, the "to" must also have a macro trigger:

``````%do i = 1 %to 5;
%do j = &i+1 %to 7;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super Contributor
Posts: 340

## Re: Do loop error problem

Hello,

%do j=i+1 to %to 7 ;

=> %do j=&i.+1 %to 7;
Occasional Contributor
Posts: 13

## Re: Do loop error problem

Thank you for your reply . I missed it . but after fix it . error does not disappear.

%macro do_loop ;
%do i=1 %to 5 ;
%do j=&i+1 to %to 7 ;

data date&i.&j. ;
set work.aa;

if 0<=a<&i then do ;
own1=1 ;
own2= 0 ;
end;
else if &i<= a =<&j then
do;
own1 = 0 ;
own2 = 0 ;
end;

else if &j < a <10 then do ;
own1 = 0 ;
own2 = 1 ;
end;
run;
proc print; run;

%end ;
%end;
%mend ;

%do_loop

Super Contributor
Posts: 340

to %to ;-)

Solution
a month ago
Super User
Posts: 10,209

## Re: Do loop error problem

To use a macro variable, you always need the ampersand, and in macro %do statements, the "to" must also have a macro trigger:

``````%do i = 1 %to 5;
%do j = &i+1 %to 7;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,209

## Re: Do loop error problem

[ Edited ]

On top of everything else, compare this with your original code:

``````%macro do_loop;
%do i = 1 %to 5;
%do j = &i + 1 %to 7;

data date&i.&j.;
set work.aa;
if 0 <= a < &i
then do;
own1 = 1;
own2 = 0;
end;
else if &i <= a =< &j
then do;
own1 = 0;
own2 = 0;
end;
else if &j < a < 10
then do;
own1 = 0;
own2 = 1;
end;
run;

proc print;
run;

%end;
%end;
%mend;
``````

and decide which is more readable and easier to maintain.

(consistent spacing and indentation)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,599

## Re: Do loop error problem

To be honest, I am looking at that code and wondering what process could posible warrant a) 5 datasets with essentially the same data, and b) that code to get to it.  Makes no sense at all.  The essence of by group processing is to add groups within the existing data, then use those rather than creating lots of copies of data, then having to code loops over it etc.

```data want;
set have;
<series of if statements>
<assign group value>
run;

proc print data=want;
by group_value;
run;
```
☑ This topic is solved.