Typically, variables in an array are processed in the sequence in which they are stored in the array. But there are times when it is beneficial to process them in reverse order.
The sample data shows that it is possible for one of the twenty-four variables in the array to have a value of 1, future values to be 0, and a value of 1 to recur more than once. The goal of this logic is to find the final occurrence of the value 1. This scenario is a good candidate for processing the array from end to beginning to identify the first time that a value of 1 occurs.
The twenty-four variables in the array represent year and month values (y02_m1 is month 1 of year 2002). The BYEAR variable represents birth year, so you would begin looking at variables in the array that are in month 1 of BYEAR+2. From that point forward, count all months until the final "1" value appears.
Click this link to watch a video about processing variables in an array in reverse order.
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.
data tables;
infile datalines truncover;
input id byear y02_m1 y02_m2 y02_m3 y02_m4 y02_m5 y02_m6 y02_m7 y02_m8 y02_m9 y02_m10 y02_m11 y02_m12 y03_m1 y03_m2 y03_m3 y03_m4 y03_m5 y03_m6 y03_m7 y03_m8 y03_m9 y03_m10 y03_m11 y03_m12;
datalines;
1 2000 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2000 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
3 2000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 2000 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
5 2000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
6 2001 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1
7 2001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0
8 2001 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
9 2001 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 0 0 0
10 2001 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 0
;
run;
data new;
set tables;
/* The last 2 digits of BYEAR are needed to compare to the year portion of the variable
names so the PUT function converts to character and the SUBSTR pulls off those 2 bytes */
year=substr(put(byear,4.),3,2);
/* Put all variables into the array */
array hold(24) y02_m1 y02_m2 y02_m3 y02_m4 y02_m5 y02_m6 y02_m7 y02_m8 y02_m9 y02_m10 y02_m11 y02_m12 y03_m1 y02_m21 y03_m3 y03_m4 y03_m5 y03_m6 y03_m7 y03_m8 y03_m9 y03_m10 y03_m11 y03_m12 ;
/* The array search begins on month 1 of the year that equals the sum of BYEAR and 2. */
/* INPUT function converts to numeric and PUT function converts to character with Zw. */
/* format so the leading zero is added to single digit years. Concatenate the leading */
/* 'y' and ending '_m1' and compare to variables using the VNAME function. */
do i=1 to 24;
if vname(hold(i))=cats('y',put(input(year,2.)+2,z2.),'_m1') then do;
/* When this match is found in the variable name, set the current value of I to PT so that can be used
in the next DO loop */
pt=i;
/* Read the variables backwards so you can know when the first '1' is found...so use reverse DO loop
from 24 to PT */
do j=24 to pt by -1;
/* When a 1 is found, compute how many months should be counted and store in TOTAL and leave the
DO loop */
if hold(j)=1 then do;
total=(j-pt)+1;
leave;
end;
end;
end;
end;
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.
/* due to width of the data set, these results show the new column, TOTAL, only */ total 24 12 1 14 24 12 5 1 7 11
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> Information ==> ARRAY |
Date Modified: | 2012-02-24 12:46:17 |
Date Created: | 2011-10-11 13:32:58 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Linux for x64 | 9.2 TS1M0 | |
Linux | 9.2 TS1M0 | |||
HP-UX IPF | 9.2 TS1M0 | |||
64-bit Enabled Solaris | 9.2 TS1M0 | |||
64-bit Enabled HP-UX | 9.2 TS1M0 | |||
64-bit Enabled AIX | 9.2 TS1M0 | |||
Windows Vista for x64 | 9.2 TS1M0 | |||
Windows Vista | 9.2 TS1M0 | |||
Microsoft Windows XP Professional | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS1M0 | |||
Microsoft® Windows® for x64 | 9.2 TS1M0 | |||
Microsoft Windows XP 64-bit Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS1M0 | |||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS1M0 | |||
z/OS | 9.2 TS1M0 | |||
OpenVMS on HP Integrity | 9.2 TS1M0 | |||
Solaris for x64 | 9.2 TS1M0 |