Passing variables to a macor that invokes another macro

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

Passing variables to a macor that invokes another macro

Hello,

I am trying to make a Macro A that invokes another Macro B. However when Macro A calls Macro B, it passes to Macro B some parameters that hae been defined in Macro A.

Here is an example of my Macro A code

/* This macro prepares data into a temporary dataset for analysis of differences between two years */

%MACRO macroA(dataSrc, recentYear, yearB);

/* First let's set a temp dataset which is unique for each calculation */

DATA Anova_&recentYear&yearB;

SET &dataSrc;

/* Then we create the calculated field for the different between the specified years */

Diff_FPS_&recentYear&yearB = FPS_&recentYear - FPS_&yearB;

LABEL Diff_FPS_&recentYear&yearB = "FUNDING PER STUDENT (&recentYear - &yearB)";

FORMAT Diff_FPS_&recentYear&yearB NLMNLGBP9.2;

Mean_GLH = (Total_GLH_&recentYear + Total_GLH_&yearB)/2;

FORMAT Mean_GLH NLMNLGBP.;

Mean_Size =.;

IF Mean_GLH < 500000 THEN Mean_Size = 5;

IF Mean_GLH >= 500000 AND Mean_GLH < 1000000 THEN Mean_Size = 4;

IF Mean_GLH >= 1000000 AND Mean_GLH < 2000000 THEN Mean_Size = 3;

IF Mean_GLH >= 2000000 AND Mean_GLH < 3000000 THEN Mean_Size = 2;

IF Mean_GLH >= 3000000 THEN Mean_Size = 1;

LABEL Mean_Size = "MEAN SIZE (&recentYear-&yearB)";

RUN;

/* now I am trying to call Macro B */

%macroB(Anova_&recentYear&yearB, Diff_FPS, Mean_Size);

%MEND onewanovaY;

My issue is how to properly pass the parameters to Macro B (highlighted in orange above). The way my code is at the moment, the name of the dataset isn't being passed correctly to Macro B.

Could someone please advise how to achieve this?

Thanks in advance for the help!

Best wishes,

P.


Accepted Solutions
Solution
‎01-26-2013 11:46 AM
Contributor
Posts: 41

Re: Passing variables to a macor that invokes another macro

Thanks but I found the mistake. The Diff_FPS variable wasn't defined because its name was dynamically set with some macro variable information. I managed to fix it.

Curiously I didn't have to do what you said. I wonder if there is two ways of achieving the same result.

Cheers,

P.

View solution in original post


All Replies
Super User
Super User
Posts: 6,498

Re: Passing variables to a macor that invokes another macro

You need to post an example of how it is not working.

You could introduce a local macro variable to macroA to allow you to build the name once.

%local dsname;

%let dsname=anova_&recentyear&yearb;

...

data &dsname ...

....

%macrob(&dsname, ....)

Solution
‎01-26-2013 11:46 AM
Contributor
Posts: 41

Re: Passing variables to a macor that invokes another macro

Thanks but I found the mistake. The Diff_FPS variable wasn't defined because its name was dynamically set with some macro variable information. I managed to fix it.

Curiously I didn't have to do what you said. I wonder if there is two ways of achieving the same result.

Cheers,

P.

Super User
Super User
Posts: 6,498

Re: Passing variables to a macor that invokes another macro

There is nothing wrong with the syntax you posted.  From your message is sounds like your real error was that the dataset was either not created or created in the wrong form to cause the next step to fail.

But in some situations referencing multiple macro variables to create a single word can be interpreted as multiple tokens.  So instead of seeing ANOVA_20122011 it is treated as if it was ANOVA_2012 and 2011. By assigning the generated string to a new variable this possibility is eliminated.  Another solution is use %unquote() around the generated string.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 201 views
  • 3 likes
  • 2 in conversation