BookmarkSubscribeRSS Feed
Obsidian | Level 7



I have a question :


I would like to create a macro %array which will do the following think:


%array(y,0, n) will return for an array which length n and beginning at time t=0.


for example:


%array(x,1, n);

%array(y,0, n);


do t = 1 to n;

x_(t) = y_(t) *3+2;


y(0) = R_0 ;

     do t = 1 to &H.;

          y(t) = x(t)+ y(t-1);

     end ;



Thank you very much for you help 

Diamond | Level 26

It's not clear to me what your question is.

Paige Miller
Obsidian | Level 7

I just want a macro which create an array by giving the name, the initial index number and the final one.


For example %array(z,0,n) will create an array which name is z and where I can go from z(0) to z(n).


Is it better?

Super User

Does the index really matter? 


SAS array statement does that by default.


So if you code:


array X(0:25) ;

 You get X1-X26 because you have 26. So in this case, using an array will not save you anything unless you're trying to initialize the  data. Since SAS uses an index of 1 compared to other languages, changing your index to 1 will simplify your life greatly as well. 


Your macro would be:


%array(z, 0, 25);

As you can see, there's very little difference here so there's no value to a macro. 


That being said, the following should be all you need, but again, it adds unnecessary complexity to your code.


%macro setUpArray(z, start, end);

array &z.(&start.:&end.) &z.&start.-&z&end.;




Super User Tom
Super User

I suspect that someone with better IML skills could show you how to do that.

But if you just want to build a matrix of literal values. Then looking at this documentation page.

You can see that you just need make a statement like this:

y={0 1 2 3} ;

So here is a macro that could do that.

%macro array(name,start,end) ;
%local i  ;
&name = {
%do i=&start %to &end; &i %end;
%mend ; 

 If course you could also just use the simple syntax like in this help page.


All arrays in the SAS/IML language are 1-based, which means that you can't use y[0].


To get started with the basics of SAS/IML, see "Ten tips for learning the SAS/IML language."


For your problem, I don't know what you are trying to accomplish, but here is some working code that you can play with to get started:


proc iml;
R_0 = 2;
x = j(n,   1, 0);   /* allocate (n x 1) vector; init to 0 */
y = j(n+1, 1, 0);   /* allocate (n+1 x 1) vector; init to 0 */

y[1] = R_0;         /* set first element to R_0 */
do t = 1 to n;
  x[t] = y[t]*3 + 2;
  do k = 2 to n;
     y[k] = x[k] + y[k-1];

print x y;
Obsidian | Level 7

Thank you for your reply.  I would like to do it without iml. I would like to create such a vector inside a data step. So if I use j(.,.,.) it won't work. Do you know how to do this?


Thank you very much 



Super User Tom
Super User

In a data step an "array" is just a convenient way to group variables. It is totally different from the real arrays and matrices that you work with in PROC IML.


What do you actually want to do. Show a small sample input dataset.  Then show what you want to do.  Show the resulting dataset.


A macro is just a tool to generate code. So show the code that you want to generate and explain what parts of it you need vary.


Since this is the SAS/IML Support Community, I assumed you wanted help with IML.


Would you like me to move this thread to the Community for DATA step programming?


Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 5 in conversation