## infinite looping

Frequent Contributor
Posts: 79

# infinite looping

Hi Experts,

I am trying to execute the folloing code but its going for the infinite looping. Can anyone help me to understand why its happening.

data test;
input Account Transaction;
cumm=0;
do until (mod(_n_,2)=0);
cumm + Transaction;
end;
datalines;
1 0
0 1
1 1
0 0
;
run;

~Rahul

Super User
Posts: 6,789

## Re: infinite looping

On your first observation, _n_=1.  So mod(_n_, 2) is also 1.

There is nothing inside your loop that changes the value of _n_.  That's where you are creating your infinite loop.

It is worth noting that _n_ is not the observation number (although it often matches the observation number in many DATA steps).  It is actually a counter, counting the number of times that the programming logic has left the DATA statement in order to execute the remaining statements within the DATA step.  Understanding _n_ requires a solid understanding of how DATA steps work.

Posts: 5,543

## Re: infinite looping

_n_ does not change in your do until() loop. So if the until condition isn't met after the first run, it is never met.

What did you intend to do?

PG
Frequent Contributor
Posts: 79

## Re: infinite looping

so what about the further iterations i.e. _n_=1,2,3...
Super User
Posts: 6,789

## Re: infinite looping

There are no further iterations.  Your program begins an infinite loop on the first iteration when _n_=1.  There is no way of getting to a further iteration.  Most likely, you would accomplish your task using this variation:

data test;
input Account Transaction;
if mod(_n_,2)=0 then cumm + Transaction;
datalines;
1 0
0 1
1 1
0 0
;

It's not 100% clear that this would be the right "solution", but it is consistent with your original program.

Super User
Posts: 13,590

## Re: infinite looping

Rahul_SAS wrote:
so what about the further iterations i.e. _n_=1,2,3...

What about them? In your original code you never leave _n_ =1. So you never the second read from the input.

What are you attempting to accomplish with that code?

Frequent Contributor
Posts: 79

## Re: infinite looping

hi pgstate,
could you please explain it...why _n_ does not change do untill() loop.

See, let me explain my prblem again...
I have 100 obs n two variables Actual and predict. I have to group them in 10 groups each and take sum.of each of 10 groups will make an output dataset with 10 observation which are grouped n summed.
as we can achieve using..
Proc Rank data=DSN groups=10;
Var Actual;
run;
and then can get the cumulative sum using first. n last.
And I want to replace proc rank with Do Until() loop.
Super User
Posts: 13,590

## Re: infinite looping

You do not want a do until because it isn't going to read new values. If you want to accumulate then maybe something more like this

``````data test;
input Account Transaction;

cumm + Transaction;
if mod(_n_,2) = 0 then do;
output;
cumm=0;
end;

datalines;
1 0
0 1
1 1
0 0
;
run;``````

BTW example data of 0 and 1 for this is pretty marginal as there are likley lots of ways to get a result that is correct for mod(2) but wouldn't work for more actual values.

Super User
Posts: 6,789

## Re: infinite looping

What if a DATA step could do this, but without using DO UNTIL or first. or last.?  Would that be acceptable?

Actually, the hardest part is deciding on the rules.  That part is up to you.  For example, if your incoming data set contains 183 observations:

• Which groups should contain 18 observations and which should contain 19 observations?
• If observations 15 through 25 contain the same actual value, should they be split up into separate groups or kept in the same group?  If they are kept in the same group, do they belong in group 1 or group 2 (or doesn't it matter)?  Extending that idea, what if 50 observations all have the same value?
Discussion stats
• 8 replies
• 431 views
• 0 likes
• 4 in conversation