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)
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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