BookmarkSubscribeRSS Feed
Linzhang426
Calcite | Level 5
Wonder if any experts can help.
I have hundreds of libraries with databases of same names in each library. My libraries are organized by dates for each month each year, such as /xxx/202301, /xxx/202302 etc.
what I need is to run a simple query against some databases within each library, but have to loop through all libraries of my choice to get data statistics. I wonder if there is a way to use a variable to do the loop, and then associate the variable with the library names.

Thanks much for your expert advice!
3 REPLIES 3
yabwon
Onyx | Level 15

What do you mean by a "database" - do you mean a single SAS dataset , or a group of datasets. 

If you mean single dataset. Does the dataset has one common name? Or are they different?

[EDIT:] Does the "xxx" means one common root directory ?

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

You say LIBRARIES in your subject line.  But in your text you appear to be talking about directories.

Do you have libref's defined that point to each of those directories?

Are you talking about SAS datasets in those different directories?  What are the names of the datasets?

 

When you have datasets in many different directories sometimes it is easier to use the actual physical name of the dataset file instead of going to the trouble of defining a series of librefs.

data all_2023;
  set "/xxx/202301/somedata.sas7bdat"
      "/xxx/202302/somedata.sas7bdat"
      "/xxx/202303/somedata.sas7bdat"
      "/xxx/202304/somedata.sas7bdat"
      "/xxx/202305/somedata.sas7bdat"
      "/xxx/202306/somedata.sas7bdat"
      "/xxx/202307/somedata.sas7bdat"
      "/xxx/202308/somedata.sas7bdat"
      "/xxx/202309/somedata.sas7bdat"
      "/xxx/202310/somedata.sas7bdat"
      "/xxx/202311/somedata.sas7bdat"
      "/xxx/202312/somedata.sas7bdat"
  ;
run;

If there is a PATTERN to the names of the directories then you can use code to generate the directory names.  For example if you wanted to generate the six digit string representing months in YYYYMM style then you could use a DO loop like this:

basedate = '01JAN2023'd;
do offset=0 to 11 ;
  date = intnx('month',basedate,offset);
  yymm = put(date,yymmn6.);
end;

Which if you needed that in macro code might look like:

%let basedate = '01JAN2023'd;
%do offset=0 %to 11 ;
  %let date = %sysfunc(intnx(month,&basedate,&offset));
  %let yymm = %sysfunc(putn(&date,yymmn6.));
%end;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 859 views
  • 0 likes
  • 4 in conversation