Contributor
Posts: 35

# using arrays

Hi,

I'm having some trouble with arrays. (full code, see below) I have two questions:

(1)

 if m < 10 then coeffm3{i,j,k,m} = coeff0&m;

=> The coeff0&m should point to a column in my table (table_coefficients which has 33 columns like COEFF01 COEFF02 ... COEFF33).

The syntax coeff0&m does not what it has to do.   It should result in COEFF01 but it doesn't.  What a I doing wrong?

(2)

I have 1 datastep which creates the array coeffm3.  After this, it uses that array to do some calculations based on the table "data_source".

It should have an output (table_results) with at least the same number of records as the table data_source.

It doesn't.

It creates a table (table_results) based on the table "table_coefficients", and not based on the table "data_source"

I want it only be based on the table "data_source".  How do I manage this?

Hopefully someone can help me...

Kind regards,

Fre

(full program

data work.table_results;

array coeffm3 {1:2,2:4,0:32,1:33} _temporary_;

set work.table_coefficients;

if type = 'N' then do;

do i = 1 to 2;

do j = 2 to 4;

do k = 0 to 32;

do m = 1 to 33;

if m < 10 then coeffm3{i,j,k,m} = coeff0&m;

else coeffm3{i,j,k,m} = coeff&m;

end;

end;

end;

end;

end;

set WORK.data_source;

g = substr(gewest,2,1);

do n=1 to (delta + 1);

coe = coeffm3(model, g, 1, n);

number_with_coeff = number * coe;

output temp9;

end;

run;

Super Contributor
Posts: 320

## Re: using arrays

First off, you are using &m, a macro variable, where I don't think you mean to.  I think you need an array - two really -

coeff0 [a bunch of variables]

coeff[a bunch of variables]

The two combined need to have 33 columns in total or more.  If they both have 33, then you can otherwise use your code as is, just change

= coeff0&m

to

= coeff0

and similar for the other reference.

The second problem is that you're confused as to what happens when you have two SET statements.  It doesn't go through one and then go through the other; it does one line of each, then the second line of each, until one of them runs out of rows.  I'm guessing table_coeffiecients runs out after a row...

You need to do something like this.  This will iterate through all of table_coefficients the first time through the data step and then return control to the data step loop, then looping normally through the second dataset.  This may be overkill (if you have only one row in table_coefficients) and it may not do precisely what you want (if you want, say, different rows in table_coefficients for different rows in data_source) but it should show you the general idea.  (I would guess you have only one row in table_coefficients, so it should work but is a bit of overkill.)

data work.table_results;

array coeffm3 {1:2,2:4,0:32,1:33} _temporary_;

if _n_ = 1 then do _pointer = 1 to nobs_coeff;

set work.table_coefficients point=_pointer nobs=nobs_coeff;

if type = 'N' then do;

do i = 1 to 2;

do j = 2 to 4;

do k = 0 to 32;

do m = 1 to 33;

if m < 10 then coeffm3{i,j,k,m} = coeff0&m;

else coeffm3{i,j,k,m} = coeff&m;

end;

end;

end;

end;

end;

end;

set WORK.data_source;

g = substr(gewest,2,1);

do n=1 to (delta + 1);

coe = coeffm3(model, g, 1, n);

number_with_coeff = number * coe;

output temp9;

end;

run;

Super User
Posts: 8,075

## Re: using arrays

You really seem to be working way too hard.

How many observations are in the TABLE_RESULTS?  From the way you constructed the first set of nested do loops it looks like there is only one and you are trying to replicate it 2*3*33 times.

Assuming the TABLE_RESULTS only has one observation then you just need a simple array.

data want;

if _n_=1 then set table_results;

array coeff coeff01 - coeff33 ;

set WORK.data_source;

g = substr(gewest,2,1);

do n=1 to (delta + 1);

coe = coeff(n);

number_with_coeff = number * coe;

output temp9;

end;

run;

If TABLE_RESULTS has multiple observations then the programming is much easier if it already has the variables needed to distinguish the observations.

model, g, k

In that case you could create a multiple dimension temporary array and populate it the first time through the data step.

Your code seems to define the third index (k) as having 33 possible values, but it only referenced the array with the third index set equal to 1.

data want;

if _n_=1 then do;

set table_results;

array coeff coeff01 - coeff33 ;

array coeffm3 {1:2,2:4,0:32,1:33} _temporary_;

do n=1 to 33;

coeffm3(model, g, k, n) = coeff(n) ;

end;

end;

set WORK.data_source;

g = substr(gewest,2,1);

do n=1 to (delta + 1);

coe = coeffm3(model, g, 1, n);

number_with_coeff = number * coe;

output temp9;

end;

run;

Contributor
Posts: 35

## Re: using arrays

thanks to you all.

it helped me further.

Discussion stats
• 3 replies
• 216 views
• 1 like
• 3 in conversation