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:
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
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
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:
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.
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
Hi Art, so far so good. Can I ask what is putn and its format z2. here?
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.
Tested the code, worked perfectly. thanks a lot!
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?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.