turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Automating execution of the same sas code n times

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2011 11:07 AM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to iank131

04-28-2011 11:19 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Cynthia_sas

04-28-2011 05:45 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to iank131

04-28-2011 11:23 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2011 05:49 PM

> %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.

> %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.