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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 1281 views
  • 1 like
  • 4 in conversation