turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- infinite looping

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-22-2016 01:02 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 01:07 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 01:13 PM

_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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 02:52 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 03:17 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 03:18 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 03:40 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 03:48 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rahul_SAS

08-22-2016 04:00 PM

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?