Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically build Libname 2

Status
Not open for further replies.

TonyH217

Programmer
Aug 22, 2007
6
GB
Hi all, I'm fairly new to SAS programming and have the following Question :-

I have about 6 SAS scripts , all of which have Libname statements coded at the start of the modules with hard-coded paths to windows folders. I have generated a CSV file which contains a name and a value of the folder path, or partial folder path which I have read into a SAS table(Params). I would now like to generate the Libname statements in the SAS module from the SAS Params table. Can anyone suggest a way to do it ?

any help much appreciated

Tony
 
Sure. Macro variables is possibly the most flexible way to do it.
If your filenames are the only thing in the file, then you'll want to do it something like this.
Code:
data _null_;
  set filenms;

  * VARNM will contain the names of the macro variables *;
  * which will be LIB1, LIB2 etc                        *;
  varnm = trim('LIB') || put(_n_,z1.);

  * filenm is the name of the variable containing the filename *;
  call symput(VARNM,filenm);
run;

* Create libraries *;

* Remember, you need to use double quotes around macro *;
* variables so that they are resolved rather than read *;
* as literal strings.                                  *;
libname lib1 "&LIB1.";
libname lib2 "&LIB2.";
* etc... *;
That shoudl do the trick for you.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks Chris - that worked very well. Libraries created OK
I am having difficulty now using the macro variables in a concatenation string e.g.

below the libname statements , I have the following currently in the code :-

%let FormatList = 'D:\Lib_Folder\Format List.csv';

One of my LIB variables (LIB2) contains D:\Lib_Folder
(with %put "&LIB2." prints "D:\Lib_Folder" in log)

I would like to use macro variable concatenated with a string as below ....

The statement :-
%Let Formatlist = "&LIB2." || '\Format List.csv';

does not resolve properly.

Once again, any help very much appreciated,

Tony

 
%LET will put exactly what you've listed in the macro variable, but resolving the macro variable, so the result of what you've put there will be something like this I believe
"D:\Lib_Folder" || '\Format List.csv'

Which should help you see what you need to do.
If you change it to
Code:
%Let Formatlist = &LIB2.\Format List.csv;


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks again Chris,

I have tried the above but have a problem still when the macro variable is used in a following data step.

I have used VAL17 in this instance which is equivalent to LIB2 in last post. Following is extract from log :-

2706 %put "&VAL17.";
"D:\Lib_Folder

"
2707
2708
2709 %let FieldList = &VAL17.\Field List.csv;
2710 %let Formatlist = &VAL17.\Format List.csv;

2713
2714 %put &Formatlist;
D:\Lib_Folder

\Format List.csv
2715 %put &Fieldlist;
D:\Lib_Folder

\Field List.csv
2716
2717

2720 %let InputLibrary = RawData;
2721
2725
2726 *Input the field list from CSV;
2727 data &InputLibrary..FIELDS ;
2728 %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
2729 infile &FieldList delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;

NOTE: Line generated by the macro variable "FIELDLIST".
1 D:\Lib_Folder
-
23
ERROR 23-2: Invalid option name :.

1 ! D:\Lib_Folder
----------
23
ERROR 23-2: Invalid option name LIB_FOLDER.

NOTE: Line generated by the macro variable "FIELDLIST".
2 \Field List.csv
-----
23
ERROR 23-2: Invalid option name FIELD.

*********************************

I am obviously doing something wrong here, but just can't figure it out....

all help apprec.

regards,

Tony

 
In an infile statement you need to use quotes. So you can do the following.
Code:
 infile "&FieldList" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;

That should work.
Klaz
 
Thanks Klaz, script works fine now ...

Many thanks to all who contributed

regards,

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top