<?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 how to change libname based on IF statement in macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296603#M62182</link>
    <description>&lt;P&gt;I am trying to change libname based on the IF statement in macro. Currently, libname is not getting assigned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;let Filename_1 = FILE1,FILE2;
%let SAS_DATASET = DATA1,DATA2;

%macro file_process;
  %let word_cnt = %sysfunc(countW(%bquote(&amp;amp;Filename_), %str(,)));
  %put NOTE: &amp;amp;=word_cnt;
%do  i = 1 %to &amp;amp;word_cnt;
%let file_name=%scan("&amp;amp;Filename_",&amp;amp;i, ",");
%let dataset=%scan("&amp;amp;SAS_DATASET",&amp;amp;i, ",");
%IF &amp;amp;file_name=File1 %Then libname xlsFile XLSX "/usr/&amp;amp;file1.xlsm";
%IF &amp;amp;file_name=File2 %Then libname xlsFile XLSX "/usr1/&amp;amp;file2.xlsm";
%end
%mend file_process;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 05 Sep 2016 22:23:57 GMT</pubDate>
    <dc:creator>jayakumarmm</dc:creator>
    <dc:date>2016-09-05T22:23:57Z</dc:date>
    <item>
      <title>how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296603#M62182</link>
      <description>&lt;P&gt;I am trying to change libname based on the IF statement in macro. Currently, libname is not getting assigned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;let Filename_1 = FILE1,FILE2;
%let SAS_DATASET = DATA1,DATA2;

%macro file_process;
  %let word_cnt = %sysfunc(countW(%bquote(&amp;amp;Filename_), %str(,)));
  %put NOTE: &amp;amp;=word_cnt;
%do  i = 1 %to &amp;amp;word_cnt;
%let file_name=%scan("&amp;amp;Filename_",&amp;amp;i, ",");
%let dataset=%scan("&amp;amp;SAS_DATASET",&amp;amp;i, ",");
%IF &amp;amp;file_name=File1 %Then libname xlsFile XLSX "/usr/&amp;amp;file1.xlsm";
%IF &amp;amp;file_name=File2 %Then libname xlsFile XLSX "/usr1/&amp;amp;file2.xlsm";
%end
%mend file_process;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Sep 2016 22:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296603#M62182</guid>
      <dc:creator>jayakumarmm</dc:creator>
      <dc:date>2016-09-05T22:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296608#M62185</link>
      <description>&lt;P&gt;Your code contains numerous errors. &amp;nbsp;They're all easy to fix, but their presence implies that you need to do some studying on the basics of using macro language.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;amp;Filename_ does not exist, you probably meant &amp;amp;Filename_1 but that's not 100% clear since you refer to &amp;amp;Filename_ twice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How could you ever find a match by comparing &amp;amp;file_name to File1? &amp;nbsp;The only possible values are entered in uppercase on your very first statement. &amp;nbsp;In macro language, File1 is different than FILE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro language does not use quotes to define character strings. &amp;nbsp;"&amp;amp;Filename_1" and "&amp;amp;SAS_DATASET" should not be in quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are asking for trouble by using a comma as a delimiter. &amp;nbsp;Blanks are usually acceptable but if your filenames might contain a blank, choose some other delimiter (not a comma).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The correct way to refer to a comma as a delimiter in macro language is %str(,). &amp;nbsp;Your choice of "," is actually treating both commas and double quotes as delimiters. &amp;nbsp;That might be a happy accident in this case, because you added quotes around "&amp;amp;Filename_" and "&amp;amp;SAS_DATASET".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your code had successfully generated a LIBNAME statement, it would not have been a complete LIBNAME statement. &amp;nbsp;It would have been missing a semicolon at the end. &amp;nbsp;The semicolons that appear now are ending the %IF/%THEN statements, and would not be part of any LIBNAME statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the %END statement is missing a semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of these are large mistakes. &amp;nbsp;But you do have some studying ahead of you.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 00:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296608#M62185</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-06T00:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296613#M62187</link>
      <description>&lt;P&gt;Thank you for you response.&lt;/P&gt;&lt;P&gt;Below is the modified code, 90%&amp;nbsp;issues you mentioned is acutally due to modified sample code from Original code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Logic:&lt;/STRONG&gt; I have different set of file name patterns so I am trying to use if statements to modify libname to accomadate the changes based on the each file_name pattern.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem in defining libname in IF statement, please let me know if there are any other way to do it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issue:&lt;/STRONG&gt; Libname is not assgined.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Filename_1 = FILE1,FILE2;
%let SAS_DATASET = DATA1,DATA2;

%macro file_process;
  %let word_cnt = %sysfunc(countW(%bquote(&amp;amp;Filename_1), %str(,)));
  %put NOTE: &amp;amp;=word_cnt;
%do  i = 1 %to &amp;amp;word_cnt;
%let file_name=%scan("&amp;amp;Filename_1",&amp;amp;i, ",");
%let dataset=%scan("&amp;amp;SAS_DATASET",&amp;amp;i, ",");
%IF &amp;amp;file_name=File1 %Then libname xlsFile XLSX "/usr/2016 log &amp;amp;file_name.xlsm";
%IF &amp;amp;file_name=File2 %Then libname xlsFile XLSX "/usr1/2016 log &amp;amp;file_name final.xlsm";
%end;
%mend file_process;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 01:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296613#M62187</guid>
      <dc:creator>jayakumarmm</dc:creator>
      <dc:date>2016-09-06T01:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296615#M62188</link>
      <description>&lt;P&gt;Well, half the errors are still there. &amp;nbsp;So I'm not sure how much help I can be until you fix the other half.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At a minimum, you will need two semicolons:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macrostatement"&gt;%IF&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;file_name&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;File1 &lt;SPAN class="token macrostatement"&gt;%Then&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;libname&lt;/SPAN&gt; xlsFile XLSX &lt;SPAN class="token string"&gt;"/usr/2016 log &amp;amp;file_name.xlsm"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That way, one semicolon can end the %IF/%THEN statement, and the second one can end the LIBNAME statement. &amp;nbsp;But the code won't work until the rest of the errors are corrected.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 01:09:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296615#M62188</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-06T01:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296620#M62191</link>
      <description>Thank you so much. It is working as expected.</description>
      <pubDate>Tue, 06 Sep 2016 02:16:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296620#M62191</guid>
      <dc:creator>jayakumarmm</dc:creator>
      <dc:date>2016-09-06T02:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296784#M62237</link>
      <description>&lt;P&gt;Although you have an answer, your code is very inefficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at the libname function within a data step which may help you avoid macro language at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've posted quite a few questions along this same example but they're all a bit scattered so far without the issues truly getting resolved. This type of code will be difficult to maintain and/or expand in the future. It may be worth starting over and explaning what you're trying to do and what's not working. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 17:27:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296784#M62237</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-06T17:27:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296799#M62241</link>
      <description>Thank you so much for helping me in solving most of the code issues. I completely agree with your point. Given below is the Logic which I am trying to achieve through SAS.&lt;BR /&gt;&lt;BR /&gt;Logic: A set of Excel workbooks(.xlsm &amp;amp; .xlsx) are uploaded into a specific directory in Linux system on monthly basis. There are no unique file name patterns are followed for each workbook but year (i.e. 2016) and prev full month(i.e. August) are the first two characters of file name.&lt;BR /&gt;&lt;BR /&gt;Data movement: Refer a particular sheet name and move the data into teradata table. Complexity in moving&lt;BR /&gt;1. reading .xlsm extension files&lt;BR /&gt;2. excel sheet names are different&lt;BR /&gt;3. Sheet name can be two separate words&lt;BR /&gt;4. column names are different in each sheet&lt;BR /&gt;5. After a sas dataset is created for each workbook move the data into a teradata table&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 06 Sep 2016 19:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296799#M62241</guid>
      <dc:creator>jayakumarmm</dc:creator>
      <dc:date>2016-09-06T19:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296864#M62265</link>
      <description>&lt;P&gt;1. Use libname methods.&lt;/P&gt;
&lt;P&gt;2. You can dynamically determine this if using libname method&lt;/P&gt;
&lt;P&gt;3. Use proc copy to import sheet, sheet name is irrelevant. Can also use NLITERAL function to create name so again not a huge issue, unless you're trying to deal with macro variables.&lt;/P&gt;
&lt;P&gt;4. Are the columns in the same order? If not how do you know which columns match up to what?&lt;/P&gt;
&lt;P&gt;5. Once that process is defined again, should be a libname and set statements.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For Step 1, this is for Windows, for Unix find the relevant command (LS). There are methods if you don't have PIPE access.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/kb/45/805.html" target="_blank"&gt;http://support.sas.com/kb/45/805.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create the macro that imports sheets from workbook and uploads to Teradata. This is the macro that is called for each iteration of the files from step 1. You can use call execute to run the macro.&lt;/P&gt;
&lt;P&gt;Unless you need to import all before the upload?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're current process is hard coding many things whereas dynamic processing would help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the file options within a PIPE method to determine the latest files in a folder if required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 23:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/296864#M62265</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-06T23:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/482593#M125031</link>
      <description>&lt;P&gt;I want to make new libref name and their new path&amp;nbsp; so that my production code is not changed when i&amp;nbsp; work on it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;***********original path*************&lt;/P&gt;&lt;P&gt;libname t "Y:\permanent\final_data.xls";&lt;BR /&gt;libname p "Y:\temporary";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;**********now i want to have a new libref for p i.e per and path will also be different , similarly for t new libref will be tem&amp;nbsp; and a diff path***********&lt;/P&gt;&lt;P&gt;%let libref = p,t;&lt;BR /&gt;%let new_libref=per,tem;&lt;BR /&gt;%macro change;&lt;BR /&gt;%let wordc= %sysfunc(countw(%bquote(&amp;amp;libref),","));&lt;/P&gt;&lt;P&gt;%put &amp;amp;wordc;&lt;BR /&gt;%do i=1 %to &amp;amp;wordc;&lt;BR /&gt;%let name = %scan("&amp;amp;libref",&amp;amp;i,",");&lt;BR /&gt;%let new_name = %scan("&amp;amp;new_libref",&amp;amp;i,",");&lt;BR /&gt;%put &amp;amp;name;&lt;BR /&gt;%put &amp;amp;new_name;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%if &amp;amp;name = p %then libname &amp;amp;new_name "Y:\&amp;amp;new_name" ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%if &amp;amp;name = t %then libname &amp;amp;new_name "Y:\&amp;amp;new_name\final_data.xls";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%mend change;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%change;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the issue is that only one libref "per" is created and not tem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;help needed&amp;nbsp; ....Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 20:05:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/482593#M125031</guid>
      <dc:creator>pri03</dc:creator>
      <dc:date>2018-07-30T20:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/482600#M125032</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/219119"&gt;@pri03&lt;/a&gt;&amp;nbsp;Please post this as a new thread entirely and you'll get better responses. These old threads will not show up on the page so only users who previously answered this question will see it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 20:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/482600#M125032</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-07-30T20:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to change libname based on IF statement in macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/482642#M125050</link>
      <description>&lt;P&gt;I'll give you a couple of starting questions, but then as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;suggested, you should post this as a new question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want a LIBNAME statement that refers to a particular file?&amp;nbsp; LIBNAME statements should refer to a folder not a file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you notice the solution to the original question on this thread?&amp;nbsp; You don't have any semicolons ending your LIBNAME statements.&amp;nbsp; You only have semicolons ending the %IF %THEN statements that begin the LIBNAME statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's not everything, but it's a good starting point.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 23:00:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-change-libname-based-on-IF-statement-in-macro/m-p/482642#M125050</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-30T23:00:27Z</dc:date>
    </item>
  </channel>
</rss>

