SAS中文论坛

标题: lag函数的用法样例 [打印本页]

作者: shiyiming    时间: 2007-9-1 07:36
标题: lag函数的用法样例
[code:5f8d9]/***************************************************************************/
/* This program generates up to three lagged values.  By increasing the    */
/* size of the array and the number of assignment statements that use      */
/* the LAGn functions, you can generate as many lagged values as needed.   */
/***************************************************************************/


/* Create starting data */  

data old;
  input start end;
datalines;
1 1
1 2
1 3
1 4
1 5
1 6
1 7
2 1
2 2
3 1
3 2
3 3
3 4
3 5
;

data new(drop=i count);
  set old;
  by start;

  /* Create and assign values to three new variables.  Use ENDLAG1-      */
  /* ENDLAG3 to store lagged values of END, from the most recent to the  */
  /* third preceding value.                                              */   
  array x(*) endlag1-endlag3;
  endlag1=lag1(end);
  endlag2=lag2(end);
  endlag3=lag3(end);

  /* Reset COUNT at the start of each new BY-Group */
  if first.start then count=1;

  /* On each iteration, set array elements that have  */
  /* not yet received a lagged value for the current  */
  /* BY-Group to missing.  Increase count by 1.       */   
  do i=count to dim(x);
    x(i)=.;
  end;
  count + 1;
run;

proc print;
run;[/code:5f8d9]




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2