DATA Step, Macro, Functions and more

Automating execution of the same sas code n times

Reply
Contributor
Posts: 53

Automating execution of the same sas code n times

Hi,

I have a dataset called DataSetA which has n observations. I have a smoothly running sas programme called ProgB. ProgB takes one observation at a time of DataSetA, beginning with the first one, does a whole bunch of stuff and updates DataSetB with a new record or changes an existing record. What I want to do is execute ProgB once for each observation of DataSetA. I am doing this manually at the moment by changing a macro variable, which is the observation number of DataSetA: manually updating the observation number: 1, 2, 3, ...,n and manually pressing the SAS programme execution button each time. I would like to automate this simple process, but I don't know how. Could you please help me?

I use PC SAS. I looked at some available tutorials and tips on SAS macros and functions, but I am overwhelmed by all the information for what I think is a really simple automation task. I have read that macros and functions cannot accommodate data steps, even the rather simple ones that I have in my ProgB (but I could be wrong). Any tips on what I can do?

Algorithmically, all I want do is:

Do i = 1 to n;
GetNextDataSetAObsvn(i); /* puts DataSetA observation i in a table with only one observation */
ExecuteProgB; /* executes lots of data steps, procs, etc on this single obsvn and updates DataSetB with the result */
End;

But I don't know how to do this in PC SAS, even after trying for many hours!

Thanks in advance,
Ian.
SAS Super FREQ
Posts: 8,864

Re: Automating execution of the same sas code n times

Hi:
This is an excellent paper to get you started:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

In particular, look at some of the later examples (Steps 7, 8, and 9), which show how to automate a DATA step and a PROC step in a macro program. Of course, you will have to understand steps 1 through 6 - -but the examples are quite good and very easy to understand.

cynthia
Contributor
Posts: 53

Re: Automating execution of the same sas code n times

Posted in reply to Cynthia_sas
> Hi:
> This is an excellent paper to get you started:
> tp://www2.sas.com/proceedings/sugi28/056-28.pdf


Hi Cynthia,

Many thanks for the paper reference. I worked through all the examples and learned a lot more from it than many other papers I looked at because this one was really step by step. Combining what I learned from this paper with the other reply I got, I have been able to solve my problem after more than a month of work on it! Thanks very much for your fast response and help!

Best wishes,
Ian.
Super Contributor
Super Contributor
Posts: 365

Re: Automating execution of the same sas code n times

Hello Iank131,
If I understood you correctly this is what you need (I used SASHELP.CLASS as an example);
[pre]
%let dsn=SASHELP.CLASS;
%macro a;
data _null_;
set &dsn obs=n end=e;
if e then call symputx('n',n);
run;
%do i=1 %to &n;
data _a;
set &dsn;
if _n_=&i;
run;

%end;
%mend a;
%a
[/pre]
Sincerely,
SPR
Contributor
Posts: 53

Re: Automating execution of the same sas code n times

> %let dsn=SASHELP.CLASS;
> %macro a;
> data _null_;
> set &dsn nobs=n end=e;
> if e then call symputx('n',n);
> run;
> %do i=1 %to &n;
> data _a;
> set &dsn;
> if _n_=&i;
> run;
>
> %end;
> %mend a;
> %a

Hi SPR,

Many many thanks for your generous help on my problem! Combined with the paper Cynthia referred me to and your suggested code, it worked brilliantly! I solved a long-standing issue I had and learned a great deal more about macros.

Thanks so much! and also for the how fast you responded!!
Regards,
Ian.
Ask a Question
Discussion stats
  • 4 replies
  • 180 views
  • 2 likes
  • 3 in conversation