BookmarkSubscribeRSS Feed
Jazzman
Obsidian | Level 7

Hi,

 

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;
    parameter=i;
  RUN;
}

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

 

 

5 REPLIES 5
Kurt_Bremser
Super User

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;
run;
%mend;

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

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
  var=a;
...

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

ID    VAR    RESULT

        A 

        B

...

 

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.   

Jazzman
Obsidian | Level 7

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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