DATA Step, Macro, Functions and more

create a macro in order to create an array

Reply
Contributor
Posts: 26

create a macro in order to create an array

Hi,

 

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 

Respected Advisor
Posts: 2,662

Re: create a macro in order to create an array

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

--
Paige Miller
Contributor
Posts: 26

Re: create a macro in order to create an array

[ Edited ]
Posted in reply to PaigeMiller

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
Posts: 22,875

Re: create a macro in order to create an array

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.;

%mend;

 

 

Super User
Super User
Posts: 7,860

Re: create a macro in order to create an array

[ Edited ]

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.

http://documentation.sas.com/?docsetId=imlug&docsetTarget=imlug_workmatrix_sect004.htm&docsetVersion...

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.

http://documentation.sas.com/?docsetId=imlug&docsetTarget=imlug_workmatrix_sect014.htm&docsetVersion...

SAS Super FREQ
Posts: 4,125

Re: create a macro in order to create an array

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;
n=10; 
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];
  end;
end;

print x y;
Contributor
Posts: 26

Re: create a macro in order to create an array

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
Super User
Posts: 7,860

Re: create a macro in order to create an array

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.

SAS Super FREQ
Posts: 4,125

Re: create a macro in order to create an array

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?

Ask a Question
Discussion stats
  • 8 replies
  • 166 views
  • 3 likes
  • 5 in conversation