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/11/25 11:57:41
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);