Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does ++ mean? 1

Status
Not open for further replies.

riskassure

Programmer
May 6, 2005
33
US
I came across a program at work that I could not decipher. I googled around and could not find any answers. Can anyone interpret the following code for me (code modified for presentation purposes):

data abc;
_n_ ++_n_ < n;
set xyz point=_n_ ;
workstn2 = workstn;
set xyz nobs=n end=end;
if workstn ¬= workstn2 and 20 <= a < 30 then a = a*0.5 ;
if workstn ¬= workstn2 and 10 <= a < 20 then a = a*0.6 ;
if workstn ¬= workstn2 and a < 10 then a = a*0.7 ;
run;

Specifically, what does the line

_n_ ++ _n_ < n;

mean? And why are there two set statements in the data step? I have a feeling that it is somehow taking two copies of the table xyz, shifting one table from the other, and comparing the variable workstn... but I am not certain, since I have never seen this before...

Thanks in advance!!

~~CW~~
 
CW,
The first line acts as a filter and will only read the observations in the given range (+ 2 records ).

The if workstn statement seems to be a not-equal operator (I haven't seen this as I use the caret (shift 6) ^= as the NE operator)

Does this help?
Klaz
 
Hi Riskassure,

This is look-ahead code.

The ++ performs the same operation as +. Some programmers prefer to use this syntax on sum statements so that it stands out. Consequently _n_ +_n_ < n will work just as well.

The second part of the expression: _n_<n resolves to a boolean value (i.e 1 or 0). If _n_ is not the last row, it will increment it by 1. I prefer to write it as _n_+(_n_<n) to highlight this behaviour.

As you may be aware, in SAS reading rows behind is straight forward using the lag function, however reading rows ahead is not so simple. This is why in this code you have two set statements. The first is to read the row ahead(starting at row 2 and ending on the last row), and the second set statement is to read the rows as normal (staring at row 1 ending at the last row).

If you try to read past the last row, SAS throws an ERROR, that's why it's important to keep track of what row is being read.

We can replicate this behaviour on the sashelp.class dataset which will be useful for others to see this behaviour in action.

Code:
data class ;
  _n_++(_n_<n) ;
  set sashelp.class point=_n_ ;
  name2 = name;    
  set sashelp.class nobs=n end=end;     
run;
proc print;run;

There are of course other ways to do this, which is discussed in more depth here:
HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top