<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Setting Libname inside a macro with Macro Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541148#M149373</link>
    <description>&lt;P&gt;Thank you Reeza.&amp;nbsp; To answer your question, my &amp;amp;fpath would resolve to "&lt;SPAN&gt;\\wilmingtonfs.corp.ad.aaamidatlantic.com\share\databasemarketing\Media Campaign\2019&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Initiative\Phone Calls\MCC\MCC-TCC Call1_18 thru 12_18.xlsx" in this partiuclar case,&amp;nbsp; However the xlsx file name changes for each file in a particular directory.&amp;nbsp; I pulled the name directly out of the %put statement in the log and hard coded it and it work, where as the use of &amp;amp;fpath in the libname statement did not.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, I was not familiar with Libname Function... A quick search landed me with this code where i substituted my names.&amp;nbsp; I am happy to say it works!!!!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;%if (%sysfunc(libname(x,&amp;amp;fpath))) %then&lt;BR /&gt;%put %sysfunc(sysmsg());&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I now have a table of Sheet names for a dynamically set excel document in my file directory... The next plan is to look through the sheet names and pull in the data... Thank you for all of your help!!!&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2019 16:42:18 GMT</pubDate>
    <dc:creator>JKrantz</dc:creator>
    <dc:date>2019-03-07T16:42:18Z</dc:date>
    <item>
      <title>Setting Libname inside a macro with Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/540938#M149289</link>
      <description>&lt;P&gt;Within a macro statement that identifies all excel files in a directory location, I'm looking to declare a libname statement for each file name so that i can pull in all sheets within each excel file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro fileinput();&lt;BR /&gt;%local i;&lt;BR /&gt;%do i= 1 %to &amp;amp;nfile;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* Store import path and output filename into macro variables*/&lt;BR /&gt;data _null_;&lt;BR /&gt;set files(firstobs= &amp;amp;i obs= &amp;amp;i);&lt;BR /&gt;/* The length of fpath can't be over 201 since windows limitation */&lt;BR /&gt;call symput('fpath', "&amp;amp;indir" || filename);&lt;BR /&gt;call symput('farch', "&amp;amp;indir.&amp;amp;outARCH.");&lt;BR /&gt;call symput('foutname', tranwrd(strip(file), ' ', '_'));&lt;BR /&gt;run;&lt;BR /&gt;%PUT &amp;amp;fpath;&lt;BR /&gt;%PUT &amp;amp;foutname;&lt;BR /&gt;%put &amp;amp;farch;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Declare File for import AND libname*/&lt;/P&gt;&lt;P&gt;FILENAME MCC_FILE "&amp;amp;fpath";&lt;BR /&gt;libname x excel "&amp;amp;fpath";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* create a table with a variable for sheetnames*/&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;create table sheet as&lt;BR /&gt;select memname&lt;BR /&gt;from dictionary.members&lt;BR /&gt;where libname='X'&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;/* Excel file import*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Until my client started putting multiple sheets in one file i used the Filename statement to pull import each separate excel file.&amp;nbsp; I think this will no longer be needed after i create the libname method.&amp;nbsp; However,&amp;nbsp; when declaring the libname using&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;libname x excel "&amp;amp;fpath";&lt;/P&gt;&lt;P&gt;Within the Macro statement i get this error:&amp;nbsp;ERROR: Error in the LIBNAME statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However if i change the statement to a hard coded path:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;libname x excel "\\wilmingtonfs.corp.ad.aaamidatlantic.com\share\databasemarketing\Media Campaign\2019&lt;BR /&gt;Initiative\Phone Calls\MCC\MCC-TCC Call1_18 thru 12_18.xlsx";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then the program works just fine creating a table with the different sheet names within the document.&amp;nbsp; However, the document names will change each time so i have a dynamic process that pulls in the files and creates the path in a macro variable to handle such an instance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can i create a libname within a macro statement using a macro variable path?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 21:45:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/540938#M149289</guid>
      <dc:creator>JKrantz</dc:creator>
      <dc:date>2019-03-06T21:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: Setting Libname inside a macro with Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/540980#M149299</link>
      <description>&lt;P&gt;What exactly is the path resolving to?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try using the libname function instead of statement - no need for a macro variable then.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265496"&gt;@JKrantz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Within a macro statement that identifies all excel files in a directory location, I'm looking to declare a libname statement for each file name so that i can pull in all sheets within each excel file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro fileinput();&lt;BR /&gt;%local i;&lt;BR /&gt;%do i= 1 %to &amp;amp;nfile;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;/* Store import path and output filename into macro variables*/&lt;BR /&gt;data _null_;&lt;BR /&gt;set files(firstobs= &amp;amp;i obs= &amp;amp;i);&lt;BR /&gt;/* The length of fpath can't be over 201 since windows limitation */&lt;BR /&gt;call symput('fpath', "&amp;amp;indir" || filename);&lt;BR /&gt;call symput('farch', "&amp;amp;indir.&amp;amp;outARCH.");&lt;BR /&gt;call symput('foutname', tranwrd(strip(file), ' ', '_'));&lt;BR /&gt;run;&lt;BR /&gt;%PUT &amp;amp;fpath;&lt;BR /&gt;%PUT &amp;amp;foutname;&lt;BR /&gt;%put &amp;amp;farch;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* Declare File for import AND libname*/&lt;/P&gt;
&lt;P&gt;FILENAME MCC_FILE "&amp;amp;fpath";&lt;BR /&gt;libname x excel "&amp;amp;fpath";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* create a table with a variable for sheetnames*/&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;create table sheet as&lt;BR /&gt;select memname&lt;BR /&gt;from dictionary.members&lt;BR /&gt;where libname='X'&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;/* Excel file import*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Until my client started putting multiple sheets in one file i used the Filename statement to pull import each separate excel file.&amp;nbsp; I think this will no longer be needed after i create the libname method.&amp;nbsp; However,&amp;nbsp; when declaring the libname using&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;libname x excel "&amp;amp;fpath";&lt;/P&gt;
&lt;P&gt;Within the Macro statement i get this error:&amp;nbsp;ERROR: Error in the LIBNAME statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However if i change the statement to a hard coded path:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;libname x excel "\\wilmingtonfs.corp.ad.aaamidatlantic.com\share\databasemarketing\Media Campaign\2019&lt;BR /&gt;Initiative\Phone Calls\MCC\MCC-TCC Call1_18 thru 12_18.xlsx";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then the program works just fine creating a table with the different sheet names within the document.&amp;nbsp; However, the document names will change each time so i have a dynamic process that pulls in the files and creates the path in a macro variable to handle such an instance.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can i create a libname within a macro statement using a macro variable path?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 01:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/540980#M149299</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-07T01:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Setting Libname inside a macro with Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541006#M149305</link>
      <description>&lt;P&gt;Create a macro to handle a single Excel file dynamically:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro excel_import(fpath);

FILENAME MCC_FILE "&amp;amp;fpath";
libname x excel "&amp;amp;fpath";

/* create a table with a variable for sheetnames*/

proc sql noprint;
create table sheet as
select memname
from dictionary.members
where libname='X'
;
quit;

/* Excel file import*/

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then, call that macro off your dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set files;
fpath = "&amp;amp;indir" || filename;
call execute('%nrstr(%excel_import(' !! trim(fpath) !! '))');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to hand further parameters to the macro, expand the definition and the call accordingly.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 06:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541006#M149305</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-07T06:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: Setting Libname inside a macro with Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541148#M149373</link>
      <description>&lt;P&gt;Thank you Reeza.&amp;nbsp; To answer your question, my &amp;amp;fpath would resolve to "&lt;SPAN&gt;\\wilmingtonfs.corp.ad.aaamidatlantic.com\share\databasemarketing\Media Campaign\2019&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Initiative\Phone Calls\MCC\MCC-TCC Call1_18 thru 12_18.xlsx" in this partiuclar case,&amp;nbsp; However the xlsx file name changes for each file in a particular directory.&amp;nbsp; I pulled the name directly out of the %put statement in the log and hard coded it and it work, where as the use of &amp;amp;fpath in the libname statement did not.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, I was not familiar with Libname Function... A quick search landed me with this code where i substituted my names.&amp;nbsp; I am happy to say it works!!!!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;%if (%sysfunc(libname(x,&amp;amp;fpath))) %then&lt;BR /&gt;%put %sysfunc(sysmsg());&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I now have a table of Sheet names for a dynamically set excel document in my file directory... The next plan is to look through the sheet names and pull in the data... Thank you for all of your help!!!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 16:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541148#M149373</guid>
      <dc:creator>JKrantz</dc:creator>
      <dc:date>2019-03-07T16:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: Setting Libname inside a macro with Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541592#M149549</link>
      <description>Once you have the library set up, use PROC COPY to copy all the datasets over easily. &lt;BR /&gt;&lt;BR /&gt;proc copy in=Excel out=work;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;You can use a data _null_ step with a call execute or dosubl to run that after you create the library.</description>
      <pubDate>Fri, 08 Mar 2019 20:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-Libname-inside-a-macro-with-Macro-Variables/m-p/541592#M149549</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-08T20:43:19Z</dc:date>
    </item>
  </channel>
</rss>

