Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Really appreciate your site. Really good site for learning what others do when they run into problems. You guy's are great!!!..."

Geography

Where in the world do Tek-Tips members come from?
riskassure (Programmer)
28 Sep 10 18:48
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~~

klaz2002 (Programmer)
8 Oct 10 11:16
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
Helpful Member!  kdt82 (Programmer)
15 Oct 10 4:58
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: http://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back

HTH

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close