/******************************************************************************
 * Programme : Dynamic Data Access: How to Automate Librefs with Macros and DLCREATEDIR
 * Reference : EXAMPL086A
 * Source    : https://www.wearecas.eu/en/sampleCode/EXAMPL086A
 ******************************************************************************/

/* --- BLOC 1 --- */
libname sales 'library-path';
data sales.quarter1;
   length mileage 4;
   input account mileage;
   datalines;
1 932
2 563
;
proc print data=sales.quarter1;
run;

/* --- BLOC 2 --- */
%macro test;
   %let mylibref=new;
   %let mydirectory=library-location;
   %if %sysfunc(libname(&mylibref,&mydirectory)) %then
      %put %sysfunc(sysmsg());
   %else %put success;
   %if %sysfunc(libref(&mylibref)) %then
      %put %sysfunc(sysmsg());
   %else %put library &mylibref is assigned to &mydirectory;
%mend test;

%test

/* --- BLOC 3 --- */
libname lib3 (lib1 lib2);

/* --- BLOC 4 --- */
options comamid=tcp;
%let myserver=host.name.com;
signon myserver.__1234 user=userid password='mypw';
libname reports '/myremotedata' server=myserver.__1234;
proc datasets library=reports;
run;
quit;
signoff myserver.__1234;

/* --- BLOC 5 --- */
libname davdata v9 "https://www.webserver.com/datadir"
        webdav user="userid" pw="12345";

/* --- BLOC 6 --- */
libname mytddata teradata server=mytera user=myid password=mypw;
data mytddata.grades;
   input student $ test1 test2 final;
   datalines;
Fred 66 80 70
Wilma 97 91 98
;
proc datasets library=mytddata;
run;
quit;

/* --- BLOC 7 --- */
libname target 'library-path';
libname mytddata teradata server=mytera user=myid password=mypw;
data target.highgrades / view=target.highgrades;
   set mytddata.grades;
   where final gt 80;
run;
proc print data=target.highgrades;
run;
proc datasets library=target;
run;
quit;

/* --- BLOC 8 --- */
options dlcreatedir;
libname mynewlib '/home/userid/mydata/project';

/* --- BLOC 9 --- */
libname mylib clear;

