l***o 发帖数: 5337 | 1 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
;
run;
data temp_1;
set old;
end_lag = lag1(end);
if start = lag1(start) then timediff = end - lag1(end);
run;
proc print;
run;
data temp_2;
set old;
end_lag = lag1(end);
if start = lag1(start) then timediff = end - end_lag;
run;
proc print;
run;
结果temp_1和temp_2不同。temp_1等于悄悄把lag1变成lag2了。
高手给点解释?谢谢。(一个包子)。 |
|
o****o 发帖数: 8077 | 2 data a;
do id=1 to 100;
a=ranuni(9999);
output;
end;
run;
data b;
set a;
lag1=lag1(a); lag2=lag2(a); lag3=lag3(a);
/*.....*/
if _n_>=3 then b3=std(of a, lag1-lag2);
if _n_>=4 then b4=std(of a, lag1-lag3);
/*.......*/
keep id a b3 b4 /*......*/;
run; |
|
h********o 发帖数: 103 | 3 Try this
===================================
PROC SQL;
CREATE TABLE NEW AS
SELECT ID,
YEAR(DATE) AS YEAR,
MONTH(DATE) AS MONTH,
SUM(COST) AS COST
FROM OLD
GROUP BY ID, YEAR, MONTH
ORDER BY ID, YEAR, MONTH;
QUIT;
DATA NEW;
SET NEW;
BY ID YEAR MONTH;
LAG1 = LAG(COST);
IF FIRST.ID THEN LAG1 = .;
LAG2 = LAG(LAG1);
IF FIRST.ID THEN LAG2 = .;
TOTAL_COST = SUM(COST, LAG1, LAG2);
KEEP ID YEAR MONTH COST TOTAL_COST;
RUN; |
|
P****D 发帖数: 11146 | 4 data one;
input Name$ 1 Order 3 Judge$ 5-7;
cards;
a 1 No
b 4 No
c
d 3 Yes
;
run;
data two(keep=name order judge);
set one;
order1=lag1(order);
judge1=lag1(judge);
if order=. and Judge="" then do;
order=order1;
judge=judge1;
end;
run; |
|
l***a 发帖数: 12410 | 5 data test;
input a b;
cards;
1 2
4 1
2 3
1 2
3 1
1 2
;
run;
data test0;
set test;
retain c;
if (lag1(b)=3 and b in (1,2)) or (lag1(b) in (1,2) and b=3) then c=a;
else c+a;
run; |
|
I**A 发帖数: 96 | 6 try the lag function:
data mydata;
set mydata;
if var_1 = . then var_1 = lag1( var_1 );
run; |
|
A*********u 发帖数: 8976 | 7 不行
首先lag记录的是上一次你用lag的时候那个var_1的值
不一定是上一个obs里var_1的值
其次,这段code不能解决有多个连续missing的情况
这样
data locf;
set olddata;
retain lastnmis; ** means last no-missing value;
if var_1>.z then lastnmis=var_1;
else var_1=lastnmis;
run;
try the lag function:
data mydata;
set mydata;
if var_1 = . then var_1 = lag1( var_1 );
run; |
|
P****D 发帖数: 11146 | 8 data b;
set a;
retain date_order;
if (date=.) then date_order=.;
else if (date~=. and lag1(date)=.) then date_order=1;
else date_order+1;
run;
? |
|
l***a 发帖数: 12410 | 9 even simpler :)
data test0;
set test;
if b>=lag1(b) then c=a;
else c+a;
run; |
|
|
|
S*****A 发帖数: 44 | 12 date value lag_value
01/01/2000 20 .
01/02/2000 50 20
01/03/2000 40 50
.......
如题,用lag function 可以得到 今天vs 昨天,如何得到今天 vs 明天呢?
试者把lag1() 写lag-1() 发现不可运行。。。。 太菜了~
有没有类似的的function 呢? |
|
D*******a 发帖数: 286 | 13 我有一组数据如下,因第二第三条记录的id缺省,想换成和第一条记录的id,我用lag
但是不行,那位高手能帮,谢了
data aa;
input id @3 vod mmddyy10. result $;
if id=. then id=lag1(id);
datalines;
1 08/11/2000 normal
. 09/23/2001 normal
. 03/01/2002 abnormal
2 05/21/2000 normal
. 04/22/2001 normal
. 11/12/2001 normal
;
run; |
|