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

 

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

6 REPLIES 6
Reeza
Super User

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!


 

Reeza
Super User
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.
ballardw
Super User

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.

tjc87
Calcite | Level 5

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

Reeza
Super User

@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.

tjc87
Calcite | Level 5

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 6 replies
  • 682 views
  • 0 likes
  • 3 in conversation