data work.earning; /*loop只发生在data步*/ value=2000; do year=1 to 20; interest=value*0.075; value+interest; /*year=21*/ end; run; proc print data=earning; run; /*升级版*/ data work.earning(drop=counter); value=2000; do counter=1 to 20; interest=value*0.075; value+interest; year+1; /*year=20*/ end; run; proc print data=earning; run; data work.earning; value=2000; do year=1 to 20; interest=value*0.075; value+interest; output; /*显示每一次执行的结果*/ /*year=1-20*/ end; run; proc print data=earning; run; /*nesting 嵌套*/ data earning; rate=0.0625/4; do year= 1 to 20; amount+2000; do quarter=1 to 4; amount+amount*rate; end; end; run; proc print data=earning; run; data work.totals(drop=i balance /*记得drop i*/ interest); set sasuser.loans; TotalInterest=0; /*可有可无*/ do i= 1 to months; /*用变量来做循环时,循环里用到的变量一直使用当前值*/ Interest=amount*(rate/12); amount+interest-payment; totalinterest+interest; end; run; proc print data=totals; run; /*每年投2000,年利率0.1,总额达到5万停手*/ data work.invest; do until(Capital>=50000); /*until至少执行一次*/ capital+2000; capital+capital*.10; Year+1; output; end; run; data retire; saving=8000; income=42000; do until(saving>1000000); income+income*0.04; saving+income*0.1; year+1; end; run; data retire; saving=8000; income=42000; do while(saving<1000000); income+income*0.04; saving+income*0.1; year+1; end; run; data work.invest(drop=i); do i=1 to 30 until(Capital>=50000); /*两个循环条件,形成类似 |或门| */ Year+1; capital+2000; capital+capital*.10; end; run; data work.subset; do sample=10 to 5000 by 10; set factory.widgets point=sample; output; end; stop; run;
Valar morghulis