代码拉取完成,页面将自动刷新
/*SASGlobalForum2011*/
/*Paper 259-2011 */
/*Choosing the Road Less Traveled: Performing Similar Tasks with either */
/*SASDATA Step Processing or with Base SASProcedures */
/*Kathryn McLawhorn, SAS Institute Inc., Cary, NC */
/*1. CALCULATING THE NUMBER OF OBSERVATIONS IN A BY GROUP*/
proc sort data=sashelp.class out=class;
by age;
run;
data count1;
set class;
by age;
if first.age then Count=0;
count+1;
if last.age then output;
keep age count;
run;
proc print data=count1 noobs;
run;
/*dow*/
data count2;
do count=1 by 1 until (last.age);
set class;
by age;
end;
keep age count;
run;
proc print data=count2 noobs;
run;
/*2.COMPUTING A TOTAL FOR A BY GROUP*/
data tot;
set class;
by age;
if first.age then do;
weight_tot=0;
end;
weight_tot+weight;
if last.age then output;
keep age weight_tot;
run;
proc print data=tot noobs;
run;
/*dow*/
data tot2 ;
do count = 1 by 1 until ( last.age ) ;
set class ;
by age ;
weight_tot = sum (weight_tot, weight) ;
end ;
keep age weight_tot;
run ;
proc print data=tot2 noobs;
run;
/*3.COMPUTING AN AVERAGE FOR A BY GROUP */
data avg (keep=age count weight_tot weight_avg);
set class ;
by age ;
retain weight_tot count;
if first.age then do;
weight_tot=0;
count=0;
end;
weight_tot + weight;
count + 1;
if last.age then do;
weight_avg=weight_tot/count;
output;
end;
run;
proc print data=avg noobs;
run;
/*dow*/
data avg2 ;
do count = 1 by 1 until ( last.age ) ;
set class ;
by age ;
weight_tot = sum (weight_tot, weight) ;
end ;
weight_avg = weight_tot / count ;
keep count age weight_tot weight_avg;
run ;
proc print data=avg2 noobs;
run;
/*4. COMPUTING A PERCENTAGE FOR A BY GROUP*/
data avg2 ;
do count = 1 by 1 until ( last.age ) ;
set class ;
by age ;
weight_tot = sum (weight_tot, weight) ;
end ;
weight_avg = weight_tot / count ;
do seq = 1 by 1 until ( last.age ) ;
set class ;
by age ;
percent=weight/weight_tot;
output;
end ;
drop sex height;
run ;
proc print data=avg2 noobs;
run;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。