Writing a do loop macro to find a string and increment

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Writing a do loop macro to find a string and increment

 

Can a global macro be created that will read in a file, increment the suffix of file name by 1 then read in the updated file name, and repeat this for a set number of files?

For example, I have 100 files that are named 'scores001' 'scores002' 'scores003'...'scores100' that need to be read into SAS then saved to a permanent 'scorestotal' file. These files have .dat extensions.

 

I've tried to use a %macro do loop but the problem is the file names are written in several places throughout my code. I would need a macro that finds the name 'scores' then appends numbers dynamically and saves the output to a permanent file.

 

The three lines of the code that need to be updated:

 

filename class &scores1;

 

data scores1;

 

if _n_=1 then set param; set scores1;

 

 

 

 

I'm using SAS 9.4 X64_8PRO platform on a Windows version 6.2.9200

 

Thanks for your time!


Accepted Solutions
Solution
‎02-08-2018 04:33 PM
Super User
Posts: 23,224

Re: Writing a do loop macro to find a string and increment

[ Edited ]

Don't. Import all in one step instead. Unless they're really big files. In that case a macro loop is quite easy:

 

%macro loop_nums(start=, end=);

    %do i=&start %to &end;

        %let index_counter = %sysfunc(putn(&i, z3.)); 

        %put &index_counter;

    %end;


%mend;

%loop_nums(start=10, end=105);

 

EDIT: Instructions on how to import all at once:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

If you're having difficulty post your code.

 


tjc87 wrote:

 

Can a global macro be created that will read in a file, increment the suffix of file name by 1 then read in the updated file name, and repeat this for a set number of files?

For example, I have 100 files that are named 'scores001' 'scores002' 'scores003'...'scores100' that need to be read into SAS then saved to a permanent 'scorestotal' file. These files have .dat extensions.

 

I've tried to use a %macro do loop but the problem is the file names are written in several places throughout my code. I would need a macro that finds the name 'scores' then appends numbers dynamically and saves the output to a permanent file.

 

The three lines of the code that need to be updated:

 

filename class &scores1;

 

data scores1;

 

if _n_=1 then set param; set scores1;

 

 

 

 

I'm using SAS 9.4 X64_8PRO platform on a Windows version 6.2.9200

 

Thanks for your time!


 

View solution in original post


All Replies
Solution
‎02-08-2018 04:33 PM
Super User
Posts: 23,224

Re: Writing a do loop macro to find a string and increment

[ Edited ]

Don't. Import all in one step instead. Unless they're really big files. In that case a macro loop is quite easy:

 

%macro loop_nums(start=, end=);

    %do i=&start %to &end;

        %let index_counter = %sysfunc(putn(&i, z3.)); 

        %put &index_counter;

    %end;


%mend;

%loop_nums(start=10, end=105);

 

EDIT: Instructions on how to import all at once:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

If you're having difficulty post your code.

 


tjc87 wrote:

 

Can a global macro be created that will read in a file, increment the suffix of file name by 1 then read in the updated file name, and repeat this for a set number of files?

For example, I have 100 files that are named 'scores001' 'scores002' 'scores003'...'scores100' that need to be read into SAS then saved to a permanent 'scorestotal' file. These files have .dat extensions.

 

I've tried to use a %macro do loop but the problem is the file names are written in several places throughout my code. I would need a macro that finds the name 'scores' then appends numbers dynamically and saves the output to a permanent file.

 

The three lines of the code that need to be updated:

 

filename class &scores1;

 

data scores1;

 

if _n_=1 then set param; set scores1;

 

 

 

 

I'm using SAS 9.4 X64_8PRO platform on a Windows version 6.2.9200

 

Thanks for your time!


 

Super User
Posts: 23,224

Re: Writing a do loop macro to find a string and increment

Replace your string with the macro variable so you're not replacing strings, the macro variable has changed and the code will loop through with that new number.
Super User
Posts: 13,283

Re: Writing a do loop macro to find a string and increment

The DAT file extension has no standard meaning so that really doesn't help. Many people and/or programs use it for "data" and are stuck back when DOS limited the extensions to 3 characters. But the content could be text, fixed column, delimited, named, or a proprietary binary format.

 

One of the "meanings" for DAT extensions was also "Digital Audio Tape" which was a high definition sound format. I doubt that is what you have though.

New Contributor
Posts: 4

Re: Writing a do loop macro to find a string and increment

The .dat files are text files with 3 rows of numeric values.

Super User
Posts: 23,224

Re: Writing a do loop macro to find a string and increment


tjc87 wrote:

The .dat files are text files with 3 rows of numeric values.


Then you should really use the approach I linked to that will import all at once and identify the source. Its the most efficient method. And no macros.

New Contributor
Posts: 4

Re: Writing a do loop macro to find a string and increment

The 'wildcard import all files in a folder' solution worked! I appreciate your help!

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 110 views
  • 0 likes
  • 3 in conversation