DATA Step, Macro, Functions and more

Macro variables.

Reply
N/A
Posts: 0

Macro variables.

A macro looking something like this will work:

%macro firstmacro;
%do i=1 %to 10;
data newtable&i;
set oldtable;
do j=1 to 5
SOMETHING...;
if SOMETHING_ELSE then output newtable&i;
end;
run;
%end;
%mend;
%firstmacro;


But if I would like to do something like this:

%macro secondmacro;
%do i=1 %to 10;
data newtable&i;
set oldtable;
array var(5);
%do j=1 %to 5
if x=var(&j) then
do;
n&j+1;
output newtable&i;
end;
%end;
run;
%end;
%mend;
%secondmacro;

In firstmacro I have a do loop with variable j, i.e. not %do.

In secondmacro I want to get n1, n2, ..., n5, so I need to code n&j.

This could probably be solved much simpler. I am just illustrating the question.

When does a variable become a macro variable inside a macro?

Does j become a macro variabel in secondmacro because of %do j... and not do j...?

Is the code in secondmacro correct?

Susan
Super Contributor
Posts: 474

Re: Macro variables.

Sorry, double posting. Message was edited by: Daniel Santos
Super Contributor
Posts: 474

Re: Macro variables.

Answering question 1.

I think you are dealing with the compile-time vs run-time dilema.

To create a macro var from within a datastep (run-time), you should use the call symput statement.
the %let statement does the same at compile-time.

Answering question 2.

Yes, on your second example j is a macro var from compile-time.

Answering question 3.

From the syntax point of view, yes. Your code is correct.
You are building 5 step cycle if-then within a 10 step cycle datastep (10x5 cycles)

So you will get 10 datasteps with 5 if-then statements in each.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
Ask a Question
Discussion stats
  • 2 replies
  • 122 views
  • 0 likes
  • 2 in conversation