BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bncoxuk
Obsidian | Level 7

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:

1 ACCEPTED SOLUTION

Accepted Solutions
Doc_Duke
Rhodochrosite | Level 12

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

8 REPLIES 8
Doc_Duke
Rhodochrosite | Level 12

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

bncoxuk
Obsidian | Level 7

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:

bncoxuk
Obsidian | Level 7

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

art297
Opal | Level 21

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

bncoxuk
Obsidian | Level 7

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

art297
Opal | Level 21

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.

bncoxuk
Obsidian | Level 7

Tested the code, worked perfectly. thanks a lot!

Howles
Quartz | Level 8

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?

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
  • 8 replies
  • 829 views
  • 3 likes
  • 4 in conversation