BookmarkSubscribeRSS Feed
Rahul_SAS
Quartz | Level 8

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

8 REPLIES 8
Astounding
PROC Star

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.

PGStats
Opal | Level 21

_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
Rahul_SAS
Quartz | Level 8
so what about the further iterations i.e. _n_=1,2,3...
Astounding
PROC Star

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.

ballardw
Super User

@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?

Rahul_SAS
Quartz | Level 8
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.
ballardw
Super User

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.

 

Astounding
PROC Star

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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 2476 views
  • 0 likes
  • 4 in conversation