Help using Base SAS procedures

How can I use a single program to run multiple data sets?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

How can I use a single program to run multiple data sets?

Hi,

The random sampling created 10 datasets. For each dataset, I will need to run the same code as below:

PROC LOGISTIC DATA=work.data01;

  CLASS age sex married;

  MODEL status=age sex married;

  OUTPUT OUT=work.data01 pred=pred reschi=reschi lower=lcl upper=ucl;

RUN;

I just don't want to repeat the same code for 10 times by just changing data01 to data02, data 03... data10.

Is there any clever way to use macro or other method to use a single piece of code to run over 10 datasets independently?

:smileyplain:


Accepted Solutions
Solution
‎07-11-2011 02:43 PM
Trusted Advisor
Posts: 2,113

How can I use a single program to run multiple data sets?

When I am only doing 10-20 iterations, I find it easier to just do some simple macro code and duplicate the call line than to write (and debug!) macro loops.  Something like

%MACRO logist(num);

PROC LOGISTIC DATA=work.data#

  CLASS age sex married;

  MODEL status=age sex married;

  OUTPUT OUT=work.data&num pred=pred reschi=reschi lower=lcl upper=ucl;

RUN;

%MEND logist;

%logist(01);

%logist(02);

%logist(03);

...

works for me.

Doc Muhlbaier

Duke

View solution in original post


All Replies
Solution
‎07-11-2011 02:43 PM
Trusted Advisor
Posts: 2,113

How can I use a single program to run multiple data sets?

When I am only doing 10-20 iterations, I find it easier to just do some simple macro code and duplicate the call line than to write (and debug!) macro loops.  Something like

%MACRO logist(num);

PROC LOGISTIC DATA=work.data#

  CLASS age sex married;

  MODEL status=age sex married;

  OUTPUT OUT=work.data&num pred=pred reschi=reschi lower=lcl upper=ucl;

RUN;

%MEND logist;

%logist(01);

%logist(02);

%logist(03);

...

works for me.

Doc Muhlbaier

Duke

Frequent Contributor
Posts: 131

How can I use a single program to run multiple data sets?

Hi Doc, thanks for sharing the idea. I don't know macro before. But this code does show the convenience and power of macro.

Great to learn! :smileylaugh:

Frequent Contributor
Posts: 131

How can I use a single program to run multiple data sets?

Doc,

Besides, can I add some iteration DO statement for the macro so that it is more convenient than typing all %logist(01), %logist(02).... This saves time if there are say 500 data sets like data01, data02, ... data500 to run the same model.Smiley Wink

PROC Star
Posts: 7,363

How can I use a single program to run multiple data sets?

Even for your 10 I would use something like:

%macro doloop;

  %do i=1 %to 10;

    PROC LOGISTIC DATA=work.data%sysfunc(putn(&i.,z2.));

      CLASS age sex married;

      MODEL status=age sex married;

      OUTPUT OUT=work.dataout%sysfunc(putn(&i.,z2.))

       pred=pred reschi=reschi lower=lcl upper=ucl;

    RUN;

  %end;

%mend doloop;

%doloop

However, that does bring up something I changed in your code.  Do you REALLY want to have the output files to have the same names as the input files?

Art

Frequent Contributor
Posts: 131

How can I use a single program to run multiple data sets?

Hi Art, so far so good. Can I ask what is putn and its format z2. here?

PROC Star
Posts: 7,363

How can I use a single program to run multiple data sets?

The putn function simply enables you to specify a numeric format at run time.  Since you indicated that the file names had leading zeros, that is why I suggested the z format, as it displays numbers with leading zeros.

Frequent Contributor
Posts: 131

How can I use a single program to run multiple data sets?

Tested the code, worked perfectly. thanks a lot!

Regular Contributor
Posts: 184

How can I use a single program to run multiple data sets?

Better to have the sampling produce one data set, with a variable to indicate the sample number. Then it's as simple as adding a BY statement to the PROC LOGISTIC step.

bncoxuk wrote:

Hi,

The random sampling created 10 datasets. For each dataset, I will need to run the same code as below:

PROC LOGISTIC DATA=work.data01;

  CLASS age sex married;

  MODEL status=age sex married;

  OUTPUT OUT=work.data01 pred=pred reschi=reschi lower=lcl upper=ucl;

RUN;

I just don't want to repeat the same code for 10 times by just changing data01 to data02, data 03... data10.

Is there any clever way to use macro or other method to use a single piece of code to run over 10 datasets independently?

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 125 views
  • 3 likes
  • 4 in conversation