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-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!

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.

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