Help using Base SAS procedures

Repeated Procedure / Loop

Posts: 0

Repeated Procedure / Loop

I have the following dataset:
obs firm
1 A
2 B
3 C
... ...
For each observation I need to carry out several procedures which I programmed already. So for example, if I take observation 1, after running my program I will obtain a dataset looking like this:
obs firm X Y ....
1 A aaa aav

What I need is a program that takes observation 1, runs the procedures, then goes on to observation 2, runs the procedures, etc. All outputs should then be combined again in a dataset looking like this:
obs firm X Y ....
1 A aaa aav
2 B dfsa dfa
3 C adf dga

I tried using the following code:
%macro match;
%Do i=1 %to 400;

data dataset_a;
set dataset_a;
if nobs ne &i then delete;

data dataset_a1;
merge dataset_a1 b;
by obs;
%mend match;

Any idea what is wrong or how I can solve my problem alternatively?
Respected Advisor
Posts: 3,899

Re: Repeated Procedure / Loop

Hi Tina

Reading your post I'm having the following thoughts:

I believe you shouldn't try to solve this problem with a macro but to use by group processing.

Just prepare your data for the procedures, sort them by observation (must be a variable like ID) and then use things like: proc print; by id; run;

You should only use a macro if you can't do it with normal SAS code. People tend too often too fast to use macros instead of re-designing their approach.

If it really has to be a macro: Look up "call execute" and how it works.

What you attempt to do is multiplying your SAS code 400 times (with minor alterations- but always accessing the same table 400 times). Even if you manage to get that working it will be bad design and perform badly.

Super User
Posts: 17,899

Re: Repeated Procedure / Loop

Use the BY Group Processing mentioned by Patrick.

As for what's wrong with the code....

In your procedure your repeated using the dataset_a but then you're deleting all observations? in the second loop the observations won't be there...

You probably want to append the data at the end, not merge as well...

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation