BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
thryce85
Fluorite | Level 6

I want to do a loop without having to go into a data step or write a macro.  I thought %do would work but it seems like I have to use that within a macro.  Are there any alternative to %do or am I stuck defining Macros and then calling them. Basically want to do this without annoyingly having to wrap it in a macro.   thanks 

 

%Macro Make_5 ;

 

%DO I = 1 %TO 5;

%put -------- D&I;
%END;


%Mend Make_5;

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

You can use conditional macro statements (%IF..%THEN...%ELSE) in open code, without a macro. However, loop iterations for a block of code does require a %macro/%mend block.

 

Don't overlook the DATA step iteration, which can be used to take an action for every observation in a data set. We often see people going straight to macro when actually a DATA step is more appropriate.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

View solution in original post

7 REPLIES 7
data_null__
Jade | Level 19

This is a non-issue issue. Just go ahead and use the macro.

AMSAS
SAS Super FREQ

%do must be in a macro

The question is what are you attempting to do? All you are doing in the code is writing a message to the log 

---D1

---D2

...

I'm guessing you want to do something more than just that, so what do you want to do?

thryce85
Fluorite | Level 6

just playing with it now .  but thinking more a long the lines of  using it along with %IF  %THEN %DO.  

sth slong the lines of .   Trying to learn data driven programming. 

 

%IF &SYSDAY=Friday %THEN %DO;

%do I=1 %to 5;

data d&i;

*some data junk;

run;

%end;

 

%END;

AMSAS
SAS Super FREQ

So ignoring the fact you are missing %MACRO/%MEND statements to define the macro
What you have here is some code to create 5 identical datasets called d1, d2, d3, d4 & d5

 

It's not the most efficient approach if creating 5 identical datasets is the goal, although it is an approach that will work.

 

 

ChrisHemedinger
Community Manager

You can use conditional macro statements (%IF..%THEN...%ELSE) in open code, without a macro. However, loop iterations for a block of code does require a %macro/%mend block.

 

Don't overlook the DATA step iteration, which can be used to take an action for every observation in a data set. We often see people going straight to macro when actually a DATA step is more appropriate.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
thryce85
Fluorite | Level 6
thanks appreciate the help
ballardw
Super User

@thryce85 wrote:

I want to do a loop without having to go into a data step or write a macro.

To do what exactly?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 924 views
  • 5 likes
  • 5 in conversation