BookmarkSubscribeRSS Feed
Nitish1003
Obsidian | Level 7
Hello everyone,
Need one help with sas programming I have folder structure "201601" i.e. YYYYMM this is from year 2016 and still going on so for every month we create new folder for that particular year. Now I want to create libname for all the folders starting from 201601 to till date (I.e. 201601 to the month when the code is getting executed) Example of i am running the code for June 2022 the libname should be created from 201601 to 202206.
Any suggestions how can I achive it. Appreciate some help here
6 REPLIES 6
ballardw
Super User

Do you want one libname for each folder or a single libname that will reference all of the folders?

Nitish1003
Obsidian | Level 7
One libname for each folder because I want to traverse each folder and check for datasets. Each folder will have 500 of datasets
ballardw
Super User

@Nitish1003 wrote:
One libname for each folder because I want to traverse each folder and check for datasets. Each folder will have 500 of datasets

You didn't provide much to go on for building paths and nothing about the actual library name (libref).

This data step lets you test specifying your basic path the part up to the YYYYMM into Libbase variable; a base for the libref in the Refbase variable.

Then uses a loop over the months to create the path and libref. You can look at the data set however you like, print or viewer.

Play with that until you get the text you want in the Libpath and Libref variables. You would then uncomment the line of code with Libname function call so that it executes and creates the libraries.

 

data example;
   length libpath $ 200 refbase $ 2 libref $ 8;
   libbase ="C:\folder\subfolder";
   /* since you intend to have 6 characters YYYYMM
      as part of the libref value the base can 
      have at most 2 characters and CANNOT start with 
      digit
   */
   refbase ="M";
   date = mdy(1,1,2016);
   do while (date le intnx('month',today(),0,'B'));
      libpath = cats(libbase,put(date,yymmn6.));
      libref  = cats(refbase,put(date,yymmn6.));
      output;
   /* rc = libname(libref,libpath); */
      date = intnx('month',date,1,'B');
   end;
run;

You do want to test this first. You may need to increase the length of the libpath if you have long paths.

SASKiwi
PROC Star

Do you have datasets of the same name in each of the monthly folders? If so a LIBNAME pointing at all folders won't work as you can't identify from which folder a particular dataset is required.

 

If datasets have the same name in each of the monthly folders, a much better data model would be to add a suffix to each dataset name  - MyDataset_YYYYMM then move them all into one folder. 

Nitish1003
Obsidian | Level 7
Thanks for looking into it. The datasets name is in XYZ_yyyymm and moving all datasets to one folder is not possible
Tom
Super User Tom
Super User

I don't really see what value you are going to get from defining all of those librefs.  You could just reference the datasets directly by their physical filename (and let SAS define its own librefs that you can ignore).

data all;
  set 
   "&topnode/201601/dataset1" 
   "&topnode/201602/dataset1" 
...
   "&topnode/202206/dataset1" 
  ;
run;

If you want to generate 201601, 201602, .... 202206 then use the INTCK() and INTNX() functions.

do offset=0 to intck('month','01JAN2016'd,'01JUN2022'd);
   date=intnx('month','01JAN2016'd,offset);
   string=put(date,yymmn6.);
   ....
end;

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