BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
DanielSantos
Barite | Level 11
Sorry, double posting. Message was edited by: Daniel Santos
DanielSantos
Barite | Level 11
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
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1136 views
  • 0 likes
  • 2 in conversation