Solved
New Contributor
Posts: 3

# Can an array index increment by 2?

Hello,

I'd like to use a SAS array to create variables for every second year  and then populate these variables. Using the example below, I'd like for the created variables to only include the even years from 1992 to 2006, for a total of 8 variables. Currently, I have to create variables for all years from 1992 to 2006, and then drop the odd numbered years.

I don't believe that a nested loop - where, for example, i = 1 to 8 - solves my problem, but please correct me if I am wrong. I think that I need the array index to only be factoring in values 1992, 1994, 1996, 1998 ...2006. This would allow the array index value to equal the _BASEYR value for which I am evaluating the expression, and populate the correct item in the array with a TRUE/FALSE value. Is it possible to specify an array index that precisely?

``````
*CREATE THE CALENDAR YEARS;
ARRAY FPB_CY (1992:2006) FPB_CY1992 - FPB_CY2006;

DO J = 1992 TO 2006 BY 2;
IF _BASEYR = J THEN FPB_CY{J}= 1;
ELSE FPB_CY{J} = 0;
END;

DROP FPB_CY1993 FPB_CY1995 FPB_CY1997 FPB_CY1999 FPB_CY2001 FPB_CY2003 FPB_CY2005;

``````

Thank you!

Preeti

Accepted Solutions
Solution
‎09-13-2016 05:48 PM
Super User
Posts: 6,754

## Re: Can an array index increment by 2?

You can do it, but you might prefer the code you have now anyway.

First, your array would have to spell out the names of all 8 elements:

array fpb_cy {8} fpb_cy1992 fpb_cy1994 fpb_cy1996 ... fpb_cy2006;

do j=1 to 8;

if _baseyr = 1990 + 2*j then fpb_cy{j}=1;

else fpb_cy{j}=0;

end;

Depending on what your are trying to achieve, it might be easier to create 8 observations, rather than a single observation with 8 new variables.  That's just to think about for the moment, since only you know where the analysis is headed.

All Replies
Solution
‎09-13-2016 05:48 PM
Super User
Posts: 6,754

## Re: Can an array index increment by 2?

You can do it, but you might prefer the code you have now anyway.

First, your array would have to spell out the names of all 8 elements:

array fpb_cy {8} fpb_cy1992 fpb_cy1994 fpb_cy1996 ... fpb_cy2006;

do j=1 to 8;

if _baseyr = 1990 + 2*j then fpb_cy{j}=1;

else fpb_cy{j}=0;

end;

Depending on what your are trying to achieve, it might be easier to create 8 observations, rather than a single observation with 8 new variables.  That's just to think about for the moment, since only you know where the analysis is headed.

New Contributor
Posts: 3