BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
egarcia
Calcite | Level 5

I have a pathway with 80+ folders that I need to create a libname for each of the 80+ folders. I currently have a SAS dataset with the name of each of the folders. The names of the folders are the date they were generated (at various times over the past year; ex: 03212013, 05052013, 09022013, 12202014, etc).

How can I tell SAS to iteratively create a libname for each observation in this dataset? All of the 80+ folders are in the same folder (i.e. C:\Users\desktop\folder\03212013, C:\Users\desktop\folder\05052013, etc.)

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

No need for macro code. Just use a data step and have it call the LIBNAME() function

You will need to define a libref for each one.  Either just generate the names sequentially or possibly use some part of the date for that.

Let's assume that you have a data set with the variable PATH that has the directories that you wan to define librefs for.

If you will only be reading from these folders then use the 'ACCESS=READONLY' option and SAS will prevent you from accidentally overwriting the existing files.

data want ;

  set have ;

  length libref $8 rc 8 sysmsg $200 ;

  libref = 'LIBR' || put(_n_,Z4.);

  rc = libname(libref,path,,'access=readonly');

  if rc ne 0 then sysmsg = sysmsg();

run;

View solution in original post

5 REPLIES 5
Ksharp
Super User

libname mylib v9 ( 'C:\Users\desktop\folder\03212013' ,  'C:\Users\desktop\folder\05052013'  .............. )  ;

But if there were a table located in multiple folds then the first fold 's table will be used .

Xia Keshan

CTorres
Quartz | Level 8

Try this macro (untested):

%macro libnames;
%let root='C:\Users\desktop\folder';

data _null_;
  set list_of_folders(keep=folder_name) nobs=nobs;      /* SAS Dataset containing the names of the folders */
  if _N_=1 then call symput("N_Libs",nobs);
  folder=cats("Folder",_N_);
  library=cats("Lib",_N_);
  lib_name=cats('L',substr(folder_name,1,4),substr(folder_name,7,2));
  call symput(folder,folder_name);
  call symput(library,lib_name);
run;

%let i=1;
%do %while (&i <= &N_Libs)
   libname &&Lib&i "&root.\&&Folder&i";
   %let i= %eval(&i+1);
%end;
%mend libnames;
%libnames

The Data _null_ step creates the 80+ macrovariables containing the librefs (i.e. L032113, L050513, etc) and folder_names.

Hope this helps,

egarcia
Calcite | Level 5

Excellent! A few small tweaks and it works perfectly. Thank you very much!

1. Remove single quotes from macro variable root

2. Add semicolon after %do %while


Tom
Super User Tom
Super User

No need for macro code. Just use a data step and have it call the LIBNAME() function

You will need to define a libref for each one.  Either just generate the names sequentially or possibly use some part of the date for that.

Let's assume that you have a data set with the variable PATH that has the directories that you wan to define librefs for.

If you will only be reading from these folders then use the 'ACCESS=READONLY' option and SAS will prevent you from accidentally overwriting the existing files.

data want ;

  set have ;

  length libref $8 rc 8 sysmsg $200 ;

  libref = 'LIBR' || put(_n_,Z4.);

  rc = libname(libref,path,,'access=readonly');

  if rc ne 0 then sysmsg = sysmsg();

run;

egarcia
Calcite | Level 5

This works just as well. Thank you all very much!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 5 replies
  • 2829 views
  • 4 likes
  • 4 in conversation