BookmarkSubscribeRSS Feed
aasdfafafsdfsaf
Calcite | Level 5

Hi,

 

I have a bunch of folders that all have a dataset with the same name in them.

folder1, folder2, folder3, folder4. all contain mydata.

 

i was wondering if it was possible to do a DO LOOP over a list of these folder names and run an extraction macro of these datasets.

 

So far all the things i've seen online are for looping over files within a folder and not looping over folders/libraries.

 

any help would be greatly appreciated thanks.

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Do you know the names of the folders in advance, or does your code have to figure this out?

--
Paige Miller
aasdfafafsdfsaf
Calcite | Level 5

I know the folders in advance.

PaigeMiller
Diamond | Level 26

So you read the known names of the folders into a SAS data set somehow (for example, by reading a text file of those names, one name on each row). Then you use CALL EXECUTE to effectively call the macro using each folder name. Something like this (you will have to replace the macro with your actual extraction macro)

 

data _null_;
    set list_of_folder_names;
    full_path_name=cats(folder_name,'\',file_name);
    call execute('%nrstr(%extract('||full_path_name||'))');
run;

 

Without more details, I can't be more specific.

--
Paige Miller
aasdfafafsdfsaf
Calcite | Level 5
Hi,

Thanks for the help. Is it possible to make a list within sas without reading in any files with folder names?
something that takes like
% let foldername = temp
libname mydir "c:\&temp"
and change the temp macroname each time for each foldername in the list?
PaigeMiller
Diamond | Level 26

Yes, it is possible.

--
Paige Miller
aasdfafafsdfsaf
Calcite | Level 5
hm okay. i'll keep looking then i guess. thanks for the help
Quentin
Super User

Are you familiar with the macro language?  Are you looking at examples of macros that use %DO loops to loop over files?  

 

If so, a key point is that a macro %DO loop that iterates over a list does not know if the list is a list of files, or directories, or elephants.  There are no 'objects' in the macro language. Everything is just text.  So the list is literally just a list of words.  


If you have an example you understand which is using the macro language to loop over a list of files, you could try revising it to loop over a list of libraries. Once you have a good understanding of macro looping, then you can loop over anything, because every list is just a list of words.  Here's one way to loop over a space-delimited list:

 

%macro try(list=) ;
  %local i item ;
  %do i=1 %to %sysfunc(countw(&list,%str( ))) ;
    %let item=%scan(&list,&i,%str( )) ;
    %put item &i: &item ;

    /**
    *here you could write any SAS code to execute for each item ; 
      libname mylib "c:\&item" ;
      data want ;
        set mylib.mydata ;
      run ;
      proc append base=wantall data=want ;
      run ;

    *this could also be a macro call e.g. :  ;
      %extract(folder=&item)
    **/

  %end ;
%mend try ;

%try(list=aaa bbb ccc)

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 8 replies
  • 2892 views
  • 1 like
  • 4 in conversation