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
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.
_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?
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.
@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?
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.
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:
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.