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

I'd like to run the same exact code on 100 different datasets.

 

So far I've been dragging manually each dataset to one particular 'library' for code execution.  I've named each dataset the same, 'xyz.sas7bdat,' to make the job easier -- no need to tailor the code for each different dataset.  After each run I move the results to a folder specifically for that particular dataset.  And then proceed on to the next dataset.  x100.

 

There's got to be a better way.

 

I'm thinking of creating a 'library' for each and every dataset:

 

libname _001 'c:\datasets\_001';

libname _002 'c:\datasets\_002';

libname _003 'c:\datasets\_003';

...

 

That way the datasets can stay put in their respective folder.  No need to manually drag anywhere.  Hopefully all code execution results will stay in the respective folder as well.

 

I can set up the code so that execution goes through all the datasets, one at a time.

 

No need to run the code separately 100 times.

 

Well, you get the drift.  Guidance greatly appreciated....

 

Thanks!

 

Nicholas Kormanik

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@NKormanik wrote:

I'm thinking of creating a 'library' for each and every dataset:

 

libname _001 'c:\datasets\_001';

libname _002 'c:\datasets\_002';

libname _003 'c:\datasets\_003';

...

 


No need to do this renaming. No need to set up a library with numeric-like names.

 

Create a text file, where the text on each line is the full folder name and file name. Read this text file into a SAS data set, then loop through each record, and do the import for the file name in each record (similar to the solution from @SASKiwi but without the renaming and renumbering).

--
Paige Miller

View solution in original post

2 REPLIES 2
SASKiwi
PROC Star

Indeed there is a better way and it is really simple. Create a controlling program to run before your main static code program.

 

Now set up your data in a single library but name each of your 100 datasets with a number suffix like xyz001, xyz002 etc. Then in the controlling program do this:

data _null_;
  do I = 1 to 100;
    call execute('data xyz; set xyz' !! put(i,z3.) !! ';run; %include MyCode.sas; run;');
  end;
run;

This code will loop 100 times, first copying the required input dataset from xyz001 (and so on) to xyz, then include and run your main program.

PaigeMiller
Diamond | Level 26

@NKormanik wrote:

I'm thinking of creating a 'library' for each and every dataset:

 

libname _001 'c:\datasets\_001';

libname _002 'c:\datasets\_002';

libname _003 'c:\datasets\_003';

...

 


No need to do this renaming. No need to set up a library with numeric-like names.

 

Create a text file, where the text on each line is the full folder name and file name. Read this text file into a SAS data set, then loop through each record, and do the import for the file name in each record (similar to the solution from @SASKiwi but without the renaming and renumbering).

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1012 views
  • 4 likes
  • 3 in conversation