Desktop productivity for business analysts and programmers

Do loop in data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Do loop in data step

Hello!

 

What is wrong with that script?

 

%LET MIN=1;
%LET MAX=1000000;

;DATA IDS(KEEP=ID);
RETAIN I;
ATTRIB ID FORMAT=12.;
I=1;
MAX=&MAX.;
DO (WHILE ID<MAX);
ID=&MIN.*I;
OUTPUT;
I+1;
END;
RUN;


Accepted Solutions
Solution
‎11-29-2017 05:05 PM
Super User
Posts: 10,530

Re: Do loop in data step

[ Edited ]
Posted in reply to kanivan51

Maxim 2: Read the log.

 

You get this:

1         MAX=&MAX.;
32         DO (WHILE ID<MAX);
                     __
                     22
ERROR: Undeclared array referenced: DO.
32         DO (WHILE ID<MAX);
                            _
                            22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, *, **, +, ',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, 
              GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, [, ^=, {, |, ||, ~=.  

ERROR 22-322: Syntax error, expecting one of the following: +, =.  

32         DO (WHILE ID<MAX);
                            _
                            76
ERROR 76-322: Syntax error, statement will be ignored.

which points to a problem with the way you wrote the do statement.

Maxim 1: Read the documentation.

The documentation for the do while statement reads:

Syntax

DO WHILE (expression);

 

which means that the opening parenthesis must come after the "while".

 

Now look at this:

%let min=1;
%let max=1000000;

data ids(keep=id);
attrib id format=12.;
I = 1;
max = &max.;
do while (id < max);
  id = &min. * i;
  output;
  i + 1;
end;
run;

and tell us which code is easier to read and understand?

Follow Maxim 12, and avoid coding in all-uppercase. The 1950's are long past.

 

BTW I removed the unnecessary retain statement. Read the documentation what it does and when it should be used.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Valued Guide
Posts: 624

Re: Do loop in data step

Posted in reply to kanivan51
  1. You code all upcase.
  2. You didn't use the {i} or running-man-icon to post the code.
  3. I don't have a crystal ball showing the log output and/or what you expected.
Occasional Contributor
Posts: 18

Re: Do loop in data step

Posted in reply to kanivan51
%LET MIN=1;
%LET MAX=1000000;

;DATA IDS(KEEP=ID);
RETAIN I;
ATTRIB ID FORMAT=12.;
I=1;
MAX=&MAX.;
DO (WHILE ID<MAX);
ID=&MIN.*I;
OUTPUT;
I+1;
END;
RUN;

err.JPG

Super User
Posts: 13,889

Re: Do loop in data step

Posted in reply to kanivan51

Post LOG information into a code box as well. Then we can cut an paste to show corrections or explanations.

Solution
‎11-29-2017 05:05 PM
Super User
Posts: 10,530

Re: Do loop in data step

[ Edited ]
Posted in reply to kanivan51

Maxim 2: Read the log.

 

You get this:

1         MAX=&MAX.;
32         DO (WHILE ID<MAX);
                     __
                     22
ERROR: Undeclared array referenced: DO.
32         DO (WHILE ID<MAX);
                            _
                            22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, *, **, +, ',', -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, 
              GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, [, ^=, {, |, ||, ~=.  

ERROR 22-322: Syntax error, expecting one of the following: +, =.  

32         DO (WHILE ID<MAX);
                            _
                            76
ERROR 76-322: Syntax error, statement will be ignored.

which points to a problem with the way you wrote the do statement.

Maxim 1: Read the documentation.

The documentation for the do while statement reads:

Syntax

DO WHILE (expression);

 

which means that the opening parenthesis must come after the "while".

 

Now look at this:

%let min=1;
%let max=1000000;

data ids(keep=id);
attrib id format=12.;
I = 1;
max = &max.;
do while (id < max);
  id = &min. * i;
  output;
  i + 1;
end;
run;

and tell us which code is easier to read and understand?

Follow Maxim 12, and avoid coding in all-uppercase. The 1950's are long past.

 

BTW I removed the unnecessary retain statement. Read the documentation what it does and when it should be used.

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

Re: Do loop in data step

Posted in reply to KurtBremser

Not sure what all the while part is for in the first place, it is a simple loop with a by statement:

data ids2;
  attrib id format=12.;
  do id=&min. to &max. by &min.;
    output;
  end;
run; 
Occasional Contributor
Posts: 18

Re: Do loop in data step

Thanks, RW9! Cool solution! I didn't know about BY statement in loop.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 814 views
  • 4 likes
  • 5 in conversation