<?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: Creating a dynamic variable in sas using macros in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639989#M190484</link>
    <description>Suppose I am using a PROC SQL command then am I supposed to write it like this:-&lt;BR /&gt;&lt;BR /&gt;PROC SQL SELECT&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;FROM &amp;amp;Country_Code._customer_list</description>
    <pubDate>Wed, 15 Apr 2020 06:50:27 GMT</pubDate>
    <dc:creator>Saurabh_Rana</dc:creator>
    <dc:date>2020-04-15T06:50:27Z</dc:date>
    <item>
      <title>Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639971#M190466</link>
      <description>&lt;P&gt;I have 3 tables with format like:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"Country_Code"_customer_list &amp;nbsp;&lt;/P&gt;&lt;P&gt;"Country_Code"_sales_data&lt;/P&gt;&lt;P&gt;"Country_Code"_payment_data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The country code in the prefex changes but the table name remains the same.For ex:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IND_customer_list &amp;nbsp;&lt;/P&gt;&lt;P&gt;IND_sales_data&lt;/P&gt;&lt;P&gt;IND_payment_data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AUS_customer_list &amp;nbsp;&lt;/P&gt;&lt;P&gt;AUS_sales_data&lt;/P&gt;&lt;P&gt;AUS_payment_data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically I want to write a single sas program for each country where I have to just change the country code once and the table names get renamed accordingly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do this using macros?I have no experience in using macros.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 05:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639971#M190466</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-04-15T05:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639974#M190469</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Start with something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro EXAMPLE(Country_Code);
%put 
&amp;amp;Country_Code._customer_list  
&amp;amp;Country_Code._sales_data
&amp;amp;Country_Code._payment_data
;
%mend EXAMPLE;

%EXAMPLE(IND)
%EXAMPLE(AUS)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and adjust according to your needs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 05:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639974#M190469</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-15T05:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639977#M190471</link>
      <description>&lt;P&gt;You should also question if it makes sense to split your data like that. A COUNTRY_CODE column in global datasets and WHERE conditions might be easier to handle.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 06:06:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639977#M190471</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-15T06:06:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639989#M190484</link>
      <description>Suppose I am using a PROC SQL command then am I supposed to write it like this:-&lt;BR /&gt;&lt;BR /&gt;PROC SQL SELECT&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;FROM &amp;amp;Country_Code._customer_list</description>
      <pubDate>Wed, 15 Apr 2020 06:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639989#M190484</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-04-15T06:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639995#M190490</link>
      <description>Yes that was my first question to the client, but for some reason they want to do it that way.&lt;BR /&gt;Anyway thanks for the suggestion</description>
      <pubDate>Wed, 15 Apr 2020 07:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/639995#M190490</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-04-15T07:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640001#M190496</link>
      <description>&lt;P&gt;Then they might consider this:&lt;/P&gt;
&lt;P&gt;Create a directory tree like this:&lt;/P&gt;
&lt;PRE&gt;/sasdata
  /sasdata/AUS
  /sasdata/IND
  .....&lt;/PRE&gt;
&lt;P&gt;Then you write all your codes as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc something data=inlib.dataset;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where INLIB is a library reference you create&amp;nbsp;&lt;EM&gt;once&lt;/EM&gt; at the start of your code like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname inlib '/sasdata/AUS';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and all follow-up code can be static, without needing any macro processing.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640001#M190496</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-15T07:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640012#M190503</link>
      <description>I am getting error while using these tables in PROC SQL</description>
      <pubDate>Wed, 15 Apr 2020 07:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640012#M190503</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-04-15T07:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640014#M190505</link>
      <description>&lt;P&gt;Please post your log, using the &amp;lt;/&amp;gt; button.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:41:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640014#M190505</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-15T07:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640016#M190507</link>
      <description>&lt;PRE&gt;&amp;lt;PROC SQL;
84 CREATE TABLE WORK.Customer_list AS
85 SELECT t1.NO_ as CUSTOMER_NO_,
86 t1.NAME,
87 /* CUSTOMER_SINCE */
88 (INPUT(SUBSTR(LEFT(t1.CUSTOMER_SINCE),1,10),yymmdd10.)) FORMAT=NLDATE10. LABEL="NLDATE10." AS CUSTOMER_SINCE,
89 t1.Customer_Status,
90 t1.CUSTOMER_POSTING_GROUP
91 FROM MYCAS.&amp;amp;._01A_CUSTOMER_LIST t1
             -
            22
            200
ERROR 22-322: Expecting a name.
ERROR 200-322: The symbol is not recognized and will be ignored./&amp;gt;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640016#M190507</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-04-15T07:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640021#M190512</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&amp;lt;PROC SQL;
84 CREATE TABLE WORK.Customer_list AS
85 SELECT t1.NO_ as CUSTOMER_NO_,
86 t1.NAME,
87 /* CUSTOMER_SINCE */
88 (INPUT(SUBSTR(LEFT(t1.CUSTOMER_SINCE),1,10),yymmdd10.)) FORMAT=NLDATE10. LABEL="NLDATE10." AS CUSTOMER_SINCE,
89 t1.Customer_Status,
90 t1.CUSTOMER_POSTING_GROUP
91 FROM MYCAS.&amp;amp;._01A_CUSTOMER_LIST t1
             -
            22
            200
ERROR 22-322: Expecting a name.
ERROR 200-322: The symbol is not recognized and will be ignored./&amp;gt;

&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What it says: it expects the name of a macro variable between the ampersand and the dot.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:54:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640021#M190512</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-15T07:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640084#M190555</link>
      <description>I am getting an error while I am using this:-&lt;BR /&gt;FROM MYCAS.&amp;amp;Country_Code._01A_CUSTOMER_LIST t1&lt;BR /&gt;&lt;BR /&gt;What exactly is the variable name in this code:-&lt;BR /&gt;&lt;BR /&gt;%macro EXAMPLE(Country_Code);&lt;BR /&gt;%put&lt;BR /&gt;&amp;amp;Country_Code._customer_list&lt;BR /&gt;&amp;amp;Country_Code._sales_data&lt;BR /&gt;&amp;amp;Country_Code._payment_data&lt;BR /&gt;;&lt;BR /&gt;%mend EXAMPLE;&lt;BR /&gt;&lt;BR /&gt;%EXAMPLE(IND)&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Apr 2020 12:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640084#M190555</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-04-15T12:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640087#M190557</link>
      <description>Let'u have one step back. Assuming that you are writing your code only for `IND` value what would the query be?&lt;BR /&gt;&lt;BR /&gt;Try to turn on MPRINT option: `options mprint;` and look into the log, what does it say?&lt;BR /&gt;&lt;BR /&gt;All the best&lt;BR /&gt;Bart</description>
      <pubDate>Wed, 15 Apr 2020 12:34:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640087#M190557</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-15T12:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640096#M190564</link>
      <description>&lt;P&gt;Country_Code is the macro &lt;EM&gt;parameter&lt;/EM&gt;; inside the macro it is used like any other macro variable.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 13:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640096#M190564</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-15T13:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640111#M190574</link>
      <description>&lt;P&gt;Sounds like you need to first understand how to use macro variables to replace part of your code.&lt;/P&gt;
&lt;P&gt;First write the code to work for one country using names like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IND_customer_list  
IND_sales_data
IND_payment_data&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in the code.&lt;/P&gt;
&lt;P&gt;Next add this line to the top of the program to define a macro VARIABLE named country_code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let country_code=IND;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and change the places in the code to use the macro variable to generate the name.&amp;nbsp; Like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp;country_code.customer_list&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that works then you might be able to just stop there.&amp;nbsp; To run the program for a different country just change the value assigned to COUNTRY_CODE macro variable at the top of the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you might want or need to to make a macro.&amp;nbsp; For that you remove the %LET and wrap the whole program inside of a macro definition that uses that macro variable name as the name of the input parameter.&amp;nbsp; And finally you need to call the macro to get it to actually run, supplying the value for country.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(country_code);
.... &amp;amp;country_code.customer_list ...
%mend mymacro;

%mymacro(IND);
%mymacro(AUS);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 13:57:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640111#M190574</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-15T13:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a dynamic variable in sas using macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640112#M190575</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes that was my first question to the client, but for some reason they want to do it that way.&lt;BR /&gt;Anyway thanks for the suggestion&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I may be&amp;nbsp; bit of&amp;nbsp; a maverick in some things but if what the client sees is what the client wants then what goes on behind the scenes shouldn't matter. Take the data, combine in a way that makes sense and create the output needed. A specified "process" that is questionable may be harder to maintain/update/extend later.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 14:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-dynamic-variable-in-sas-using-macros/m-p/640112#M190575</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-15T14:13:09Z</dc:date>
    </item>
  </channel>
</rss>

