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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 640 views
  • 0 likes
  • 2 in conversation