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.)
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;
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
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,
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
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;
This works just as well. Thank you all very much!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.