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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 505 views
  • 0 likes
  • 4 in conversation