How can you use an array in a macro?
I cannot define an array in a macro, so I decided to make the array in a datastep and reference to it:
data _null_; x1 = 1; x2 = 2; array bonds{2} x1-x2; call symput('blub', bonds); run; %macro test(input=); %let var = 1; %put 1; %put &var; %mend test; %test(input=&blub);
It fails at the symput:
ERROR: Illegal reference to the array bonds.
It's just some dummy code.
Let's assume:
%macro test(input=); %let var = input{1}; %put &var; %mend test;
Can I pass my bonds array to this macro?
Call Symput in data step is equivalent to %LET, that is assing value to a macro variable;
In your code "bonds" is a reference name of an array, not a variable value.
You can try a macro similar to that:
%macro test(array=1 2 3);
%let i = 1;
%do %until(&x = );
%let x = %scan(input, &i);
%put I = &i x = &x;
%let i = %eval(&i +1);
%end;
%mend;
%test
Here is the repaired code to test:
%macro test(array=1 2 3);
%let i = 1;
%do %until(&x = );
%let x = %scan(&array, &i);
%put I = &i x = &x;
%let i = %eval(&i +1);
%end;
%mend;
%test;
Thanks for your reply.
I still have one problem. The array is defined outside the macro in a data step.
Whenever I try to 'call symput', I get the error 'Illegal reference to the array' so I cannot use it as a parameter.
Any suggestions?
Something suboptimal:
array=&var1-&var10
Where var... is defined outside the macro (with call symput).
In this case, the vars need to be filled with some dummy data which is not what I want.
Then you can do:
data _NULL_;
x1 = 1; x2=2;
array bonds{2} x1-x2;
len = length(bonds);
length array_string = $20;
array_string = left(x1);
do i=2 to len;
array_straing = trim(array_string) || ' '||left(x(i));
end;
call symput ('my_array', trim(array));
run;
then use the macro test as:
%test(array = &my_array);
sorry, next code runs without errors:
data _NULL_;
x1 = 1; x2=2;
array bonds{2} x1-x2;
len = dim(bonds);
length array_string $20;
array_string = left(x1);
do i=2 to len;
array_straing = trim(array_string) || ' '||left(bonds(i));
end;
call symput ('my_array', trim(array_string));
run;
Hi
To put all values of an array into one string, you can use the CAT... functions together with the OF keyword and an arrayname. See example below.
data _NULL_;
x1 = 1;
x2 = 2;
x3 = 3;
array bonds{*} x1-x3;
length array_string $20;
array_string = catx(" ", of bonds{*});
call symputx('my_array', array_string);
run;
%put NOTE: &=my_array;
Bruno
Based on your other question about importing multiple excel workbooks/sheets I can't help but suggest that this is the wrong path to be going down.
You need to provide the file paths. SAS can extract them from a directory or you can provide the path or generate them in some manner, assuming some logic in naming, to pass to your proc import. An array will not help in any way with this process.
If you have the paths in a datastep you can call the import procedure macro via a call execute statement.
Thanks for this!
Let me loop through an array/variable list to execute a nested macro I created.
i'm sure there are other approaches but this was simple enough.
***Since I was using this to execute a nested macro I created, I changed the code a little to only run til the end of the list- instead of when x was null.
%macro test(array=Var1 Var2 Var3 .... );
%let i = 1;
%do %until( &i = %eval(%sysfunc(countw(&array.)) +1));
%let x = %scan(&array, &i);
%put I = &i x = &x;
%let i = %eval(&i +1);
%MyNestedMacro(varn= &x);
%end;
%mend;
%test;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: