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!
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:
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!
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:
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!
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.
The .dat files are text files with 3 rows of numeric values.
@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.
The 'wildcard import all files in a folder' solution worked! I appreciate your help!
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.