BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lakhpreet
Calcite | Level 5

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 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

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.

Lakhpreet
Calcite | Level 5
Thank you! I actually really like this solution. It's quite tidy and informative on how to use that same concept in other array situations.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1683 views
  • 0 likes
  • 2 in conversation