SAS retain 语句data sample;input ID $ type income;cards;1 1 2001 2 3001 3 1002 1 2002 3 1002 4 100;run;data new_sample;set sample;by id;retain new_income;if first.id then new_income=0;new_income+income;if last.id then output;drop income type;run;da
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/22 15:50:52
![SAS retain 语句data sample;input ID $ type income;cards;1 1 2001 2 3001 3 1002 1 2002 3 1002 4 100;run;data new_sample;set sample;by id;retain new_income;if first.id then new_income=0;new_income+income;if last.id then output;drop income type;run;da](/uploads/image/z/8313636-12-6.jpg?t=SAS+retain+%E8%AF%AD%E5%8F%A5data+sample%3Binput+ID+%24+type+income%3Bcards%3B1+1+2001+2+3001+3+1002+1+2002+3+1002+4+100%3Brun%3Bdata+new_sample%3Bset+sample%3Bby+id%3Bretain+new_income%3Bif+first.id+then+new_income%3D0%3Bnew_income%2Bincome%3Bif+last.id+then+output%3Bdrop+income+type%3Brun%3Bda)
SAS retain 语句data sample;input ID $ type income;cards;1 1 2001 2 3001 3 1002 1 2002 3 1002 4 100;run;data new_sample;set sample;by id;retain new_income;if first.id then new_income=0;new_income+income;if last.id then output;drop income type;run;da
SAS retain 语句
data sample;
input ID $ type income;
cards;
1 1 200
1 2 300
1 3 100
2 1 200
2 3 100
2 4 100
;
run;
data new_sample;
set sample;
by id;
retain new_income;
if first.id then new_income=0;
new_income+income;
if last.id then output;
drop income type;
run;
data new_sample2;
set sample;
by id;
if first.id then new_income=0;
new_income+income;
if last.id then output;
drop income type;
run;
为什么这两个程序结果一样?体现不出来retain的作用啊?
SAS retain 语句data sample;input ID $ type income;cards;1 1 2001 2 3001 3 1002 1 2002 3 1002 4 100;run;data new_sample;set sample;by id;retain new_income;if first.id then new_income=0;new_income+income;if last.id then output;drop income type;run;da
累加语句new_income+income;相当于下面的程序段:
retain new_income 0;
new_income=sum(new_income,income);
所以说,上面的两个程序都使用了retain的功能,结果当然一样!
想体现出retain的作用,可以试试把累加语句new_income+income;改为
new_income=sum(new_income,income);