BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have the following dataset:
datset_a
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;
run;
%end;
%mend match;
%match;

Any idea what is wrong or how I can solve my problem alternatively?
Thanks.
2 REPLIES 2
Patrick
Opal | Level 21
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.

HTH
Patrick
Reeza
Super User
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...

HTH,
Reeza

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 948 views
  • 0 likes
  • 3 in conversation