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
It's not clear to me what your question is.
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?
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;
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;
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;
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
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?
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.
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.