How do I iteratively create multiple libnames using a do loop?

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How do I iteratively create multiple libnames using a do loop?

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.)


Accepted Solutions
Solution
‎07-09-2014 10:11 AM
Super User
Super User
Posts: 7,076

Re: How do I iteratively create multiple libnames using a do loop?

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


All Replies
Super User
Posts: 10,044

Re: How do I iteratively create multiple libnames using a do loop?

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

Regular Contributor
Posts: 180

Re: How do I iteratively create multiple libnames using a do loop?

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,

New Contributor
Posts: 4

Re: How do I iteratively create multiple libnames using a do loop?

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


Solution
‎07-09-2014 10:11 AM
Super User
Super User
Posts: 7,076

Re: How do I iteratively create multiple libnames using a do loop?

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;

New Contributor
Posts: 4

Re: How do I iteratively create multiple libnames using a do loop?

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 658 views
  • 3 likes
  • 4 in conversation