/***************************************************************************************************************** SAS file name: DoW_Loop File location: _________________________________________________________________________________________________________________ Purpose: To demonstrate the DoW Loop in SAS Author: Peter Clemmensen Creation Date: 01/01/2019 This program supports the example page "The Magic of the SAS DoW Loop by Example" on SASnrd.com *****************************************************************************************************************/ /* Example Data */ data MyData; input ID var @@; datalines; 1 10 1 30 1 50 2 40 2 60 2 70 2 40 3 80 ; /* A simple DoW Loop */ data DowLoop; do _N_=1 by 1 until (last.ID); /* 1 */ set MyData; /* 2 */ by ID; /* 3 */ sum=sum(sum, var); /* 4 */ end; run; /* The Double DoW Loop */ data Double_DowLoop; do _N_=1 by 1 until (last.ID); set MyData; by ID; sum=sum(sum, var); end; avg=divide(sum, _N_); do until (last.id); set MyData; by id; output; end; run; /* Equivalent to */ proc sql; create table dow_SQL as select *, sum(var) as sum, mean(var) as avg from MyData group by id order by id; quit; /* Remember: When a data step encounters an empty data set buffer, the data step terminates. Therefore test3 has only 3 observations */ data test1; input x @@; datalines; 1 2 3 4 5 6 7 8 9 10 ; data test2; input y @@; datalines; 1 2 3 ; data test3; set test1;put (_N_)(=); set test2;put (_N_)(=); run;