DATA Step, Macro, Functions and more

Very simple question

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Very simple question

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!!!!


Accepted Solutions
Solution
‎08-06-2015 10:35 AM
Super User
Super User
Posts: 7,079

Re: Very simple question

Posted in reply to Caroline1620_

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)

View solution in original post


All Replies
Super User
Super User
Posts: 7,079

Re: Very simple question

Posted in reply to Caroline1620_

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.

Super User
Super User
Posts: 7,997

Re: Very simple question

Posted in reply to Caroline1620_

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


Solution
‎08-06-2015 10:35 AM
Super User
Super User
Posts: 7,079

Re: Very simple question

Posted in reply to Caroline1620_

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)

Super User
Posts: 19,875

Re: Very simple question

Posted in reply to Caroline1620_

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;



New Contributor
Posts: 4

Re: Very simple question

Thank you so much for your help! This works!!

Kind Regards,  Tekla

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 300 views
  • 4 likes
  • 4 in conversation