<?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: Calling unknown numbers of Macro Variables in a Data Step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448313#M283449</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. n is some number that I generated before this macro definition. Sorry I didn't make my question clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically my question is, we cannot have macro %do loop in a data step. What is the work around if we have to call a macro names depends on the DO index within a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or alternatively, is there any way to set a %if(variable condition) outside a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you. Really appreciate you guys' helps.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Mar 2018 20:04:59 GMT</pubDate>
    <dc:creator>sas_cc</dc:creator>
    <dc:date>2018-03-23T20:04:59Z</dc:date>
    <item>
      <title>Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448297#M283446</link>
      <description>&lt;P&gt;Hi SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find&amp;nbsp;the dummy code&amp;nbsp;with my question in below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%do i = 1 %to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; data _null_;&amp;nbsp;&lt;FONT color="#339966"&gt;Create macro variables name1, name2, ... name&amp;lt;n&amp;gt; from a pre-made data set according to the variable i.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set&amp;nbsp;DS1&amp;amp;i end = final;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;call symput('name' || strip(_n_),strip(names));&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if FINAL then call symput('total',_n_);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; data test&amp;amp;i;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; set DS2; &lt;FONT color="#339966"&gt;There was already a variable "assign" with some values filled in DS2. I plan to fill out the blanks with the value of the macro &amp;amp;&amp;amp;name&amp;amp;m. Checking if&amp;nbsp;&lt;SPAN&gt;&amp;amp;&amp;amp;name&amp;amp;m satisfies certain condition&lt;/SPAN&gt; in ascending order, if not, check&amp;nbsp;&lt;SPAN&gt;&amp;amp;&amp;amp;name%eval(&amp;amp;m+1) and so on, until the blank is filled then exist the do loop (with m).&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; retain assign;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; do m = 1 to &amp;amp;total;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if missing(assign) = 1 and _n_ = &amp;amp;i then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assign1 = "&amp;amp;&amp;amp;name&amp;amp;m";&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; run;&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The condition for &amp;amp;&amp;amp;name&amp;amp;m couldn't be checked outside of the data step, because the value of the DS2 is involved in the condition checking.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone please help? Or if there's any other approaches that you suggest. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 19:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448297#M283446</guid>
      <dc:creator>sas_cc</dc:creator>
      <dc:date>2018-03-23T19:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448303#M283447</link>
      <description>&lt;P&gt;I think you want to expand a little bit on what this is actually supposed to be doing in terms of purpose and best with small example of dataset DS2 and as many DS1x as you think this is supposed to process.&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am pretty sure that you have multiple errors starting with&lt;/P&gt;
&lt;P&gt;%do i = 1 %to n;&lt;/P&gt;
&lt;P&gt;As n is a character value, does not represent a macro variable but is used in a macro loop. So what is n supposed to represent and where is the value supposed to come from?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 19:42:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448303#M283447</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-23T19:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448304#M283448</link>
      <description>&lt;P&gt;Do you have non macro working code? That should be your starting point&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 19:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448304#M283448</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-23T19:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448313#M283449</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. n is some number that I generated before this macro definition. Sorry I didn't make my question clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically my question is, we cannot have macro %do loop in a data step. What is the work around if we have to call a macro names depends on the DO index within a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or alternatively, is there any way to set a %if(variable condition) outside a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you. Really appreciate you guys' helps.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 20:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448313#M283449</guid>
      <dc:creator>sas_cc</dc:creator>
      <dc:date>2018-03-23T20:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448317#M283450</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;we cannot have macro %do loop in a data step&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can, but it depends.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 20:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448317#M283450</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-23T20:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448318#M283451</link>
      <description>&lt;P&gt;I think you might be taking a difficult approach.&amp;nbsp; While I can't test this right now (and would really like to, as I suspect there are tweaks needed here and there), here's the approach I would use.&amp;nbsp; Here's what goes inside the outer %DO loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;/P&gt;
&lt;P&gt;select quote(names) into : name_list separated by ' ' from DS1&amp;amp;i;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test&amp;amp;i;&lt;/P&gt;
&lt;P&gt;array list {&amp;amp;sqlobs} _temporary_ $ 32 (&amp;amp;name_list);&lt;/P&gt;
&lt;P&gt;set DS2;&lt;/P&gt;
&lt;P&gt;length assign1 $ 32;&lt;/P&gt;
&lt;P&gt;if missing(assign)=1 then do _n_=_n_ to &amp;amp;sqlobs until (assign1 &amp;gt; ' ');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;assign1 = list{_n_};&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure if you intended to create a new variable (ASSIGN1) instead of just replacing ASSIGN.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Mar 2018 20:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/448318#M283451</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-23T20:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/449093#M283452</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In what case we are / are not able to do so?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 18:27:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/449093#M283452</guid>
      <dc:creator>sas_cc</dc:creator>
      <dc:date>2018-03-27T18:27:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/449094#M283453</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Astounding,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your helps. I took a different approach based on your suggestions!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2018 18:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/449094#M283453</guid>
      <dc:creator>sas_cc</dc:creator>
      <dc:date>2018-03-27T18:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calling unknown numbers of Macro Variables in a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/449126#M283454</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/99987"&gt;@sas_cc&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your reply. n is some number that I generated before this macro definition. Sorry I didn't make my question clear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically my question is, we cannot have macro %do loop in a data step. What is the work around if we have to call a macro names depends on the DO index within a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or alternatively, is there any way to set a %if(variable condition) outside a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you. Really appreciate you guys' helps.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can use the SYMGET() function to retrieve the value of a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mvar1=one;
%let mvar2=two;
data test ;
  do i=1 to 2;
     value = symget(cats('mvar',i));
     put i= value= ;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Mar 2018 19:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-unknown-numbers-of-Macro-Variables-in-a-Data-Step/m-p/449126#M283454</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-27T19:24:24Z</dc:date>
    </item>
  </channel>
</rss>

