BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Caroline1620_
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Tom
Super User Tom
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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


Tom
Super User Tom
Super User

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)

Reeza
Super User

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;



Caroline1620_
Calcite | Level 5

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

Kind Regards,  Tekla

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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