DATA Step, Macro, Functions and more

Compile file names into a single batch file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Compile file names into a single batch file

I used SAS to create 60,000 flexMIRT syntax files for a simulation study. Now I need to compile those file names into a single csv batch file. I've created the batch macro below but I keep overwriting the batch file instead of amending it as I try and loop through the file names. I think I need to create a DO loop within the batch macro. The batch file should look like:

C:\Users\KMarino\300_10_0.11_10_1.flexmirt

C:\Users\KMarino\300_10_0.11_10_2.flexmirt

C:\Users\KMarino\300_10_0.11_10_3.flexmirt

......

C:\Users\KMarino\300_10_0.11_10_250.flexmirt

.....

C:\Users\KMarino\1000_10_0.11_10_1.flexmirt

.....

C:\Users\KMarino\300_10_0.11_10_250.flexmirt


How can I modify the macro below to appropriately compile all the files?


%MACRO batch(I=, ss=, cl=, bv=, it=, rep=);

DATA _NULL_;

      FILE "C:\Users\KMarino\Dropbox\Dissertation\Data Generation\For Dissertation\SAS Control Files\flexMIRTsim.BATRUN.csv";

             PUT #&I @1 "C:\Users\KMarino\&ss._&cl._&bv._&it._&rep..flexMIRT"

;

RUN;

%mend batch;

data _null_;

DO ss=300, 1000, 5000;

DO cl=10, 60, 100, 150;

      DO it=10, 20, 50, 70;

     DO BV=0.053, 0.11, 0.25, 0.43, 1.00

       DO it=10, 20, 50, 70;

                   Do rep=1 to 250;

               _N_+1;

                              call execute (catt('%batch (I='|| put(_N_+1,2.)||', ss=', ss ,', cl=', cl ,', bv=',bv,', it=',it,', rep=',rep,')')) ;

                       end;

          end;

         end;

        end;

     end;

    end;

run;


Accepted Solutions
Solution
‎05-14-2015 06:59 PM
Super Contributor
Super Contributor
Posts: 3,174

Re: Compile file names into a single batch file

Add the operand "MOD" to your FILE statement - it will be ignored by SAS on the first execution of the invoked DATA _NULL_ step.

Scott Barry
SBBWorks, Inc.

View solution in original post


All Replies
Super User
Posts: 11,341

Re: Compile file names into a single batch file

If this a program that will run under SAS it may be easier to use something like:

%include "C:\Users\KMarino\*.flexmirt";

Solution
‎05-14-2015 06:59 PM
Super Contributor
Super Contributor
Posts: 3,174

Re: Compile file names into a single batch file

Add the operand "MOD" to your FILE statement - it will be ignored by SAS on the first execution of the invoked DATA _NULL_ step.

Scott Barry
SBBWorks, Inc.

Contributor dkb
Contributor
Posts: 53

Re: Compile file names into a single batch file

Alternatively it could be done in a single data step without using a macro, something like this :

data _null_;

FILE "C:\Users\KMarino\Dropbox\Dissertation\Data Generation\For Dissertation\SAS Control Files\flexMIRTsim.BATRUN.csv";

DO ss='300', '1000', '5000';

DO cl='10', '60', '100', '150';

  DO it='10', '20', '50', '70';

   DO BV='0.053', '0.11', '0.25', '0.43', '1.00';

    DO it='10', '20', '50', '70';

     Do rep=1 to 250;

      string= compress('C:\Users\KMarino\' !! ss !! '_' !! cl !! '_' !! bv !! '_' !! it !! '_' !! put(rep, 3.) !! '.flexMIRT', ' '); 

      PUT  @1 string;

     end;

    end;

   end;

  end;

end;

end;

run;

Occasional Contributor
Posts: 7

Re: Compile file names into a single batch file

This worked perfectly except it compressed the file name and deleted the spaces. The true path is C:\Users\KMarino\Dropbox\Dissertation\Data Generation\For Dissertation\Simulation Syntax\filename.flexMIRT.

It deleted the space between "Data Generation", "For Dissertation" and "Simulation Syntax". Is there a way to avoid that?

Respected Advisor
Posts: 3,799

Re: Compile file names into a single batch file

You might thing about what is happening when you COMPRESS.

Super User
Super User
Posts: 7,038

Re: Compile file names into a single batch file

1) Why not make the list while you make the files?  Either as a data set or into the text file.

2) Since you already have the files why not just use DIR command to get the list?  Does the order matter?  And if so is it different than the order that DIR will return.

data _null_;

  file

"C:\Users\KMarino\Dropbox\Dissertation\Data Generation\For Dissertation\SAS Control Files\flexMIRTsim.BATRUN.csv";

infile 'dir /b C:\Users\KMarino\*.flexmirt' pipe;

  input;

  put _infile_;

run;

Trusted Advisor
Posts: 3,211

Re: Compile file names into a single batch file

Use the mod option with the file-statement when you do not want to overwrite but append the csv file SAS(R) 9.4 Statements: Reference, Third Edition

---->-- ja karman --<-----
Occasional Contributor
Posts: 7

Re: Compile file names into a single batch file

Thanks for all the suggestions! The MOD statement worked after some further modification of the original syntax but now I've also learned some new commands from the other suggestions. Thanks so much for the help!

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 439 views
  • 6 likes
  • 7 in conversation