1 Star 0 Fork 0

yzw554350785/Programming-SAS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DOW_datasteps.sas 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
Jiangtang Hu 提交于 2015-09-24 13:28 . bulk upload
/*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;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yzw554350785/Programming-SAS.git
git@gitee.com:yzw554350785/Programming-SAS.git
yzw554350785
Programming-SAS
Programming-SAS
master

搜索帮助