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.

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

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.

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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