Macro questions

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

Macro questions

Hello:

Is it possible to include a macro variable within a macro?

i.e. for the following code (in bold, below) could I replace the last macro variable covar with &covar_list where

I'd then use: "%let covar_list = Firm_employee_n ; "?

Many thanks in advance!

%macro meansd(var, set, level, covar);

data estabgroup;

set tab_estabgroup;

if estabgroup="amp" then estabgroup_amp_Firm_employee_n_meansd=Firm_employee_n_mean||" ("||Firm_employee_n_std||")";

run;

%mend meansd;

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level=Control, covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level=EPPM, covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level=OSHA, covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level=SDT, covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level=SafetyPays, covar=Firm_employee_n);

%meansd (var=mode, set=tab_mode, level=Control, covar=Firm_employee_n);

%meansd (var=mode, set=tab_mode, level=Email, covar=Firm_employee_n);

%meansd (var=mode, set=tab_mode, level=Mail, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=Control, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=Current, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=OSHA, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=Future, covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level=Myth/Fact, covar=Firm_employee_n);


Accepted Solutions
Solution
‎04-14-2015 01:47 PM
Trusted Advisor
Posts: 1,612

Re: Macro questions

Is it possible to include a macro variable within a macro?

Yes

i.e. for the following code (in bold, below) could I replace the last macro variable covar with &covar_list where

I'd then use: "%let covar_list = Firm_employee_n ; "?

If fail to see the point of this, it seems like unnecessary work that provides no benefit.

Is there any real reason to replace

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=Firm_employee_n);

with

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=&covar_list); ???

I see no reason to do this, its just takes you longer to program it (it's one more line of code)

View solution in original post


All Replies
Super User
Posts: 10,500

Re: Macro questions

All of your code is bold so it is hard to pick out what you want.

Also your Macro makes no use of ANY of the parameters. So I'm not sure what you want.

IF the macro were to work, what is the desired code that should be the result? Provide a more detailed example.

Frequent Contributor
Posts: 131

Re: Macro questions

Hi ballardw,

I didn't include the correct code, so, it's included below.


My question is, if I create a macro variable called covar_list which is, namely, a list of covariates, could I use it in the following macro?

%macro meansd(var, set, level, covar);

data &var.;

set &set.;

if &var.=&level. then &var._&level._&covar._meansd=&covar._mean||" ("||&covar._std||")";

run;

%mend meansd;

%meansd (var=estabgroup, set=tab_estabgroup, level="amp", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="sst", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="hih", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="nur", covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level="Control", covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level="EPPM", covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level="OSHA", covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level="SDT", covar=Firm_employee_n);

%meansd (var=message, set=tab_message, level="SafetyPays", covar=Firm_employee_n);

%meansd (var=mode, set=tab_mode, level="Control", covar=Firm_employee_n);

%meansd (var=mode, set=tab_mode, level="Email", covar=Firm_employee_n);

%meansd (var=mode, set=tab_mode, level="Mail", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="Control", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="Current", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="OSHA", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="Future", covar=Firm_employee_n);

%meansd (var=estabgroup, set=tab_estabgroup, level="Myth/Fact", covar=Firm_employee_n);

Trusted Advisor
Posts: 1,612

Re: Macro questions

Everywhere you use

covar=Firm_employee_n

Yes of course you can replace it with any macro variable or regular variable name, but what is the point? In your example, you are replacing constant text that never varies with some other constant text that never varies. That's not why you would use a macro variable.

Frequent Contributor
Posts: 131

Re: Macro questions

Hi PaigeMiller: You're right in thinking that from the code I posted.  I m asking because I ll eventually have to update the program to create more analysis variables for each variable in a list of covariates. Right now, the only covariate I have is the firm_employee_n var.   So, if

%let covar_list = Firm_employee_n covar2 covar3 covar4;

I could revise the macro so the last macro variable covar references &covar_list, right?

Thanks!

Trusted Advisor
Posts: 1,612

Re: Macro questions

You'd have to revise your macro too, it won't automatically loop over the different terms in covar_list. See https://communities.sas.com/thread/74943

Solution
‎04-14-2015 01:47 PM
Trusted Advisor
Posts: 1,612

Re: Macro questions

Is it possible to include a macro variable within a macro?

Yes

i.e. for the following code (in bold, below) could I replace the last macro variable covar with &covar_list where

I'd then use: "%let covar_list = Firm_employee_n ; "?

If fail to see the point of this, it seems like unnecessary work that provides no benefit.

Is there any real reason to replace

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=Firm_employee_n);

with

%meansd (var=estabgroup, set=tab_estabgroup, level=amp, covar=&covar_list); ???

I see no reason to do this, its just takes you longer to program it (it's one more line of code)

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 258 views
  • 6 likes
  • 3 in conversation