BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ayin
Quartz | Level 8
/* 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.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

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.

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ayin
Quartz | Level 8
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?
Reeza
Super User

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};
Reeza
Super User

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

Ksharp
Super User
What are you trying to do ?

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2257 views
  • 1 like
  • 4 in conversation