Sample 55272: Process groups of variables in N increments within an array
When variables need to be processed in groups, using an array with several DO loops is a simple approach.
See the Full Code tab for sample code that processes three variables at a time to determine whether they all have a value of 1. When that condition is true, create a new variable whose value is 1.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This data set contains 15 variables, Day1-Day15. Three variables should be processed together to determine whether they all have a value of 1. When that condition is true, create a new variable whose value is 1.
/* Create a sample data set */
data test;
input (day1-day15) (:1.);
datalines;
1 1 1 0 1 0 1 0 1 0 0 0 1 1 1
0 0 1 0 0 0 1 1 1 1 0 1 0 0 0
1 0 1 1 0 1 0 1 0 0 1 0 1 1 1
;
run;
data new;
set test;
/* Put all variables into an array for ease of referencing */
array meds(450)day1-day15;
/* New variables that will contain 1 or 0 values */
array bi(5);
/* Outer DO loop that goes from 1 to 15 */
do i=1 to (15/3);
/* Set TOTAL to 0 before each inner loop so its value is reset after being accumulated */
total=0;
/* Process every three variables */
do j=((i*3)-2) to (i*3);
/* Sum each value of Meds(j) with TOTAL */
total + meds(j);
end;
/* If every variable had a value of 1, the total will be 3, so set the new variable to 1 */
if total=3 then bi(i)=1;
/* Otherwise set it to 0 */
else bi(i)=0;
/* Drop unwanted variables */
drop i total j;
end;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
d d d d d d
d d d d d d d d d a a a a a a
O a a a a a a a a a y y y y y y b b b b b
b y y y y y y y y y 1 1 1 1 1 1 i i i i i
s 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 1 2 3 4 5
1 1 1 1 0 1 0 1 0 1 0 0 0 1 1 1 1 0 0 0 1
2 0 0 1 0 0 0 1 1 1 1 0 1 0 0 0 0 0 1 0 0
3 1 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1
Date Modified: | 2015-03-02 14:16:54 |
Date Created: | 2015-02-25 10:35:32 |
Operating System and Release Information
SAS System | Base SAS | z/OS | 9 TS M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |
Microsoft Windows 2000 Advanced Server | 9 TS M0 | |
Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |
Microsoft Windows 2000 Server | 9 TS M0 | |
Microsoft Windows 2000 Professional | 9 TS M0 | |
Microsoft Windows NT Workstation | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |
Microsoft Windows XP Professional | 9 TS M0 | |
64-bit Enabled AIX | 9 TS M0 | |
64-bit Enabled HP-UX | 9 TS M0 | |
64-bit Enabled Solaris | 9 TS M0 | |
HP-UX IPF | 9 TS M0 | |
Linux | 9 TS M0 | |
OpenVMS Alpha | 9 TS M0 | |
Tru64 UNIX | 9 TS M0 | |