DATA Step, Macro, Functions and more

loops in SAS

Occasional Contributor
Posts: 12

loops in SAS



is there a feasible way (or a good alternative) to do loops in SAS without getting into macros? I need to repeat the same analysis section (a sequence of proc steps) for a lot of variables which are named with single letters in alphabetical order. So I would like to do something like this:


Loop that executes the following code for each i=C to K {
  PROC whatever;

What would be the easiest way of doing this in SAS?



Super User
Posts: 10,239

Re: loops in SAS

[ Edited ]

If you need to run a proc or data step repeatedly, you can put your parameter values in a dataset or a do loop and use call execute() to run the code.

Depending on the complexity, it might still be useful to wrap the code into a macro:

%macro do_this(i);
proc whatever;
parameter = &i;

data _null_;
do i = &c to &k;
  call execute('%do_this('!!trim(put(i,best.))!!');');

Here the macro is only use to make the call execute less complicated.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: loops in SAS

Its often a question that comes up, generally this is due to choices made in the strcuture of the data.  Sure you can mash some macro code around the code you have and then call it for each variable, that will work, you could also call execute it.  However, a simple change to your data structure will change the way you look at the data and make your code far simpler.  Say you have this dataset:
ID    A    B    C


Now you could do:

proc means data=have;

For each variable, or you could normalise that data (i.e. long rather than wide) to look like:






Then your means code becomes:

proc means data=have;
by var; var=result; ...

No need to loop or write any further code.  By group processing is one of the least used and most powerful functions of SAS.   

Occasional Contributor
Posts: 12

Re: loops in SAS

Hi RW9,


thanks for your reply, that definitely sounds interesting. However my impression is that this causes more problems than it solves. To begin with, to restructure the data like this isn't done with a single command in SAS, right? Especially since multivariate analyses would require a multileveled data structure. And after you prepared your data you have to rethink every single procedure. For instance every multivariate model has to be transformed into a unilinear model with repeated measures.


I hope my reservations are understandable. Do you really recommend to work with long file formats if you have to do a large number of analyses?

Super User
Super User
Posts: 9,599

Re: loops in SAS

Thats quite difficult to say, I don't know your data, your processes etc.  To your question on the normalisation, to normalise/transpose data you can use the proc transpose procedure.  If it is a complicated transpose/normalise then use array's/datastep processing. 

Wether this structure is good for your analysis - I don't know, I have no information.  What I can say is that SAS is built around the concept of:

- The datastep, which is in itself a loop

- By group processing

Your original post:

"is there a feasible way (or a good alternative) to do loops in SAS without getting into macros"

This would be you creating code to replicate what a datastep does

"I need to repeat the same analysis section"

This sounds like you need to do code for various sub-groups.


At the end of the day its up to you and your decision based on the facts you know, I merely respond to the point:

"s there a feasible way (or a good alternative) to do loops in SAS without getting into macros?"

Yes, there are various methods.  Data structure change, arrays, using inbuilt shortcuts such as VAR: or VAR1--VAR10 or _numeric_.  Or you could use macros, or call execute.  

Super User
Posts: 23,724
Ask a Question
Discussion stats
  • 5 replies
  • 4 in conversation