| 
 | 
楼主
 
 
 楼主 |
发表于 2012-2-22 23:49:07
|
只看该作者
 
 
 
为什么LAG失效
在SAS DUMMY看到的: 
[code:mxpft3mp]data test; 
  infile datalines dlm=',' dsd; 
  input a b c; 
  datalines; 
4272451,17878,17878  
4272451,17878,17878  
4272451,17887,17887  
4272454,17878,17878  
4272454,17881,17881  
4272454,17893,17893  
4272455,17878,17878  
4272455,17878,18200  
run; 
  
data testLags; 
  retain e f ( 1 1); 
  set test; 
  if a=lag(a) and b>lag(b) then 
    e=e+1; 
  else if a^=lag(a) or lag(a)=. then 
      e=1; 
  if a^=lag(a) or lag(a)=. then 
      f=1; 
  else if a=lag(a) and b>lag(b) then 
      f=f+1; 
run; 
  
proc print data=testLags; 
run;[/code:mxpft3mp] 
 
还是没明白,为什么F的值在第五行就没有累加??? 
 
原文解释是:Let's review how the LAG function works. It draws values from a queue of previous values, and within each DATA step iteration that you call the LAG function, it draws a previous value from the queue. The trick here is that this program does not call the LAG function for both A and B with each iteration of the DATA step! Because the IF statements combine two conditions with an AND, if the first condition resolves to false, the second condition is not evaluated. After all, in logic-speak, FALSE AND (ANY value) is always FALSE, so the DATA step can save work by not bothering to evaluate the remainder of the expression. 
 
  if a=lag(a) /* if false*/ and b>lag(b) /* then this is not evaluated*/ 
And then the next time around, when the LAG(b) function is called again, it's "behind" one on the queue for the value of b |   
 
 
 
 |