Building an array looping 3 variables

Reply
New Contributor
Posts: 4

Building an array looping 3 variables

I'm trying to build a 2 dimension array using the following logic:

Row is populated by variable Year

Column is populated by variable Month

The value is a number following a repeating sequence; '01','02','03','07','04','05','06'

i.e. If we start at Year=14, Month=04 then the following values should be assigned:

xarray[04,14]='01';

xarray[05,14]='02';

xarray[06,14]='03';

xarray[07,14]='07';

xarray[08,14]='04';

xarray[09,14]='05';

xarray[10,14]='06';

xarray[11,14]='01';

xarray[12,14]='02';

xarray[01,15]='03';

I'd like to loop this code to create the array 12 years out. I'm having a hard time retaining the order of the sequence to assign value. Any assistance would be appreciated.

Thanks!

Super User
Posts: 17,912

Re: Building an array looping 3 variables

What are you trying to do overall?

I can see the loop back to the new year being confusing, but there may be a better way to set up the logic.

Super User
Posts: 5,093

Re: Building an array looping 3 variables

I think you have trapped yourself by considering this to be a two-dimensional array.  All arrays, no matter how many dimensions they have, are merely a collection of some of the variables.  Switch to a one-dimensional array:

array m {144};

do i=1 to 144;

   remainder = mod(i, 7);

   if remainder = 1 then m{i} = '01';

   else if remainder = 2 then m{i} = '02';

   else if remainder = 3 then m{i} = '03';

   else if remainder = 4 then m{i} = '07';

   else if remainder = 5 then m{i} = '04';

   else if remainder = 6 then m{i} = '05';

   else m{i} = '06';

end;

If you want to bring these variables into a two-dimensional array afterwards, SAS won't complain.  You may need to expand your two-dimensional array to include 13 years, with placeholder variables for the first 3 and last 9 elements.

New Contributor
Posts: 4

Re: Building an array looping 3 variables

I want to be able to access the 2 digit number using the month and year. i.e. %let two_digit_code=xarray[month(date()),year(date())];

Super User
Posts: 5,093

Re: Building an array looping 3 variables

That comes later, after you have assigned values.  For example:

array xarray {12, 2014:2028} $ 2 dummy1-dummy3 m1-m144 dummy4-dummy12;

Then a DATA step can use

xarray{month_value, year_value} to retrieve any particular element based on the month and year.  But that won't happen using macro language.  The definition of an array is temporary ... once the DATA step is over, the array is gone.

Ask a Question
Discussion stats
  • 4 replies
  • 256 views
  • 0 likes
  • 3 in conversation