## Assign values for a two-dimensional array

Solved
Frequent Contributor
Posts: 82

# Assign values for a two-dimensional array

``````/* dataset */
_NAME_ new1 new2 new3 new4
Time_0   1   2    3    4
Time_1   5   6    7    8
Time_2   9   10   11   12
Time_3   13  14   15   16``````

I have a dataset, and need to use a two-dimensional array - temprg{i,j} for some data manipulation.

how to assign value to this array?

Most online SAS tutorials use Datalines, but since I have much more rows and columns, I'm looking for another way to assign the initial values. e.g. temprg{2,3} would be 7. temprg{4,2} would be 14.

Thanks.

Accepted Solutions
Solution
‎03-20-2017 06:59 PM
Posts: 1,387

## Re: Assign values for a two-dimensional array

[ Edited ]

Since you don't post your data in the form of a sas data set, I presume you want to make an array out of 4 observations.  Then what do you want to do?  Do you want to generate a collection of results for every 4 incoming rows?  if so, you can start with this:

data _null_;

array temprg {0:3,4};

array new {4};

do row=0 to 3;

set have;

do col=1 to 4; temprg{row,col}=new{col};  /*temprg{row}=new{col};*/  end;

end;

** Now use the array to generate results **;

run;

BTW, I've indexed the rows as 0 to 3, not 1 to 4.  Since you have _NAME_=0,1,2,3.

All Replies
Solution
‎03-20-2017 06:59 PM
Posts: 1,387

## Re: Assign values for a two-dimensional array

[ Edited ]

Since you don't post your data in the form of a sas data set, I presume you want to make an array out of 4 observations.  Then what do you want to do?  Do you want to generate a collection of results for every 4 incoming rows?  if so, you can start with this:

data _null_;

array temprg {0:3,4};

array new {4};

do row=0 to 3;

set have;

do col=1 to 4; temprg{row,col}=new{col};  /*temprg{row}=new{col};*/  end;

end;

** Now use the array to generate results **;

run;

BTW, I've indexed the rows as 0 to 3, not 1 to 4.  Since you have _NAME_=0,1,2,3.

Frequent Contributor
Posts: 82

## Re: Assign values for a two-dimensional array

Thanks mkeintz for replying. Used your codes but it reports an error: Too few array subscripts specified for array temprg. (under the line 'temprg{row} = new{col})'. Not sure what caused the problem?
Super User
Posts: 23,932

## Re: Assign values for a two-dimensional array

Data step 'arrays' don't function like they would in IML, ie no tranpsose or inverting them via functions, only via looping manually.

I really think your logic is simple if you took the time to state it out. I'm not going to try and decipher it though.

When referencing a two dimensional array, you need two indexes. I think this corrects the issue.

``temprg{row, col}=new{col};``
Super User
Posts: 23,932

## Re: Assign values for a two-dimensional array

If you matrix functionality I suggest using SAS IML instead of Base.

Super User
Posts: 10,846

## Re: Assign values for a two-dimensional array

```What are you trying to do ?

Reeza is absolutely right. Use IML code if you want some matrix operator.

```
☑ This topic is solved.