Hi I am a novice and have a simple question. How do I create an array with text. I have a macro that needs to run over a series of variables
Like this (which runs fine)
%weighted_mean_std (m_vikt_livs1, fr_nki);
%weighted_mean_std (m_vikt_livs1, fr_for);
%macro a;
data test;
array alla{2} fr_nki fr_for;
%do i=1 %to 2;
%weighted_mean_std (m_vikt_livs1,alla(i));
%end;
%mend;
%a;
But this does not work because the array is wrong it creates a series of empty arrays out of what I would like to be the content of the array.
alla does not resolve
Thanks for help!!!!
Did you mean to code something like this?
%macro a(varlist);
%local i ;
%do i=1 %to %sysfunc(countw(&varlist));
%weighted_mean_std (m_vikt_livs1,%scan(&varlist,&i));
%end;
%mend;
%a(fr_nki fr_for)
We cannot evaluate without knowing what the macro does.
Turn on the MPRINT option so you can see what code your macro calls are generating.
If the macro generates anything other than data step statements then you cannot use it inside of a data step.
Simple answer - you are mixing up macro code and datastep code:
Either change this:
%weighted_mean_std (m_vikt_livs1,alla(&i.));
Or just remove the macro code alltogether:
data test;
array alla{2} fr_nki fr_for;
do i=1 to 2;
%weighted_mean_std (m_vikt_livs1,alla(i));
end;
run;
However, as Tom has stated, this depends on what the macro is doing. I would have thought that you need to assign the value somehow?
alla(i) = %weighted_mean_std (m_vikt,livs1);
Did you mean to code something like this?
%macro a(varlist);
%local i ;
%do i=1 %to %sysfunc(countw(&varlist));
%weighted_mean_std (m_vikt_livs1,%scan(&varlist,&i));
%end;
%mend;
%a(fr_nki fr_for)
Put your variables in a dataset and then use call execute.
Untested:
data parameters;
input param1 $ param2 $ ;
cards;
m_vikt_livs1 fr_nki
m_vikt_livs1 fr_for
;
run;
data _null_;
set parameters;
str=cats('%weighted_mean_std(', param1, ',', param2, ')');
call execute(str);
run;
Thank you so much for your help! This works!!
Kind Regards, Tekla
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 25. Read more here about why you should contribute and what is in it for you!
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.