DATA Step, Macro, Functions and more

do loop: variable list and while/until ?

Reply
Frequent Contributor
Posts: 100

do loop: variable list and while/until ?

Experts:
Should we try to avoid using a variable list and the while / until condition together? It looks like the while / until does not kick in till the last variable value anyway.
Thanks.
data work.Test;
Amount=50;
do pay_time = 1,2,3,4 while (Amount LE 100);
Amount+ 50;
output;
end;
run;
proc print data=work.Test;
run;
data work.Test1;
Amount=50;
do pay_time = 1,2,3,4 until (Amount GE 100);
Amount+ 50;
output;
end;
run;
proc print data=work.Test1;
run;
Respected Advisor
Posts: 3,777

Re: do loop: variable list and while/until ?

I don't know what you should avoid. If you read the documentation carefully you will see that each comma separated specification can have it's own while/until clause.

[pre]
specification
denotes an expression or a series of expressions in this form

start
[/pre]

Very powerful!!
Super Contributor
Super Contributor
Posts: 3,174

Re: do loop: variable list and while/until ?

The SAS log below, using a PUTLOG, not an OUTPUT, demonstrates clearly the SAS operational behavior with each:

1 data work.Test;
2 Amount=50;
3 do pay_time = 1,2,3,4 while (Amount LE 100);
4 Amount+ 50;
5 putlog _all_;
6 end;
7 run;

Amount=100 pay_time=1 _ERROR_=0 _N_=1
Amount=150 pay_time=2 _ERROR_=0 _N_=1
Amount=200 pay_time=3 _ERROR_=0 _N_=1
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds


8 /*
9 proc print data=work.Test;
10 run;
11 */
12 data work.Test1;
13 Amount=50;
14 do pay_time = 1,2,3,4 until (Amount GE 100);
15 Amount+ 50;
16 putlog _all_;
17 end;
18 run;

Amount=100 pay_time=1 _ERROR_=0 _N_=1
Amount=150 pay_time=2 _ERROR_=0 _N_=1
Amount=200 pay_time=3 _ERROR_=0 _N_=1
Amount=250 pay_time=4 _ERROR_=0 _N_=1
NOTE: The data set WORK.TEST1 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: do loop: variable list and while/until ?

Hello,

There is a difference between do i=1 to 4 while... and do i=1,2,3,4 while...



in the second one the expression is evaluated only for the last item (i=4).

in the first one the expression is evaluated at the beginning of each iteration.



Consider the differences between these examples:



[pre]
data work.Test;
Amount=50;
do pay_time = 1,2,3,4 while (Amount LE 100);
Amount+ 50;
output;
end;
run;
data work.Test1;
Amount=50;
do pay_time = 1 to 4 while (Amount LE 100);
Amount+ 50;
output;
end;
run;

proc print data=work.Test;
run;
proc print data=work.Test1;
run;
[/pre]

HTH,


Marius

Frequent Contributor
Posts: 100

Re: do loop: variable list and while/until ?

Thanks to all three of you! I did not know we could combine While/Until with individual index variable values, multiple times ...in the do statement. Tested a few. I have to agree with Data _null_ that it is powerful. A nice surprise!
Ask a Question
Discussion stats
  • 4 replies
  • 234 views
  • 0 likes
  • 4 in conversation