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
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;
Then your DO loop would become more difficult to read:
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.
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;
Then your DO loop would become more difficult to read:
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: