<?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 Consolidate user input for a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596278#M171663</link>
    <description>&lt;P&gt;Num is a variable that's being defined two times, the first time outside the macro and later as a macro input. I'd like to reduce the number of times that a user will enter data. Any suggestions for how to consolidate the two inputs into one?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(The data step inside the macro can stand on its own, but I'm using this as an example for a larger macro that I'm working with.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num = 3; /*input by user*/

data time;
input time1 - time&amp;amp;num;
infile datalines stopover; /*STOPOVER writes ERROR to sas log file if dataline values &amp;lt; num*/
datalines;
24 40 60
;
run;


%macro example(num);
data new;
   set time;
   array utime (&amp;amp;num) time1 - time&amp;amp;num;
      do i = 1 to &amp;amp;num;
         utime[i] = utime[i] + 1;
      end;
   drop i;
run;
%mend;

%example(num=3); /*input by user*/
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 14 Oct 2019 14:19:00 GMT</pubDate>
    <dc:creator>thewan</dc:creator>
    <dc:date>2019-10-14T14:19:00Z</dc:date>
    <item>
      <title>Consolidate user input for a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596278#M171663</link>
      <description>&lt;P&gt;Num is a variable that's being defined two times, the first time outside the macro and later as a macro input. I'd like to reduce the number of times that a user will enter data. Any suggestions for how to consolidate the two inputs into one?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(The data step inside the macro can stand on its own, but I'm using this as an example for a larger macro that I'm working with.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num = 3; /*input by user*/

data time;
input time1 - time&amp;amp;num;
infile datalines stopover; /*STOPOVER writes ERROR to sas log file if dataline values &amp;lt; num*/
datalines;
24 40 60
;
run;


%macro example(num);
data new;
   set time;
   array utime (&amp;amp;num) time1 - time&amp;amp;num;
      do i = 1 to &amp;amp;num;
         utime[i] = utime[i] + 1;
      end;
   drop i;
run;
%mend;

%example(num=3); /*input by user*/
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 14:19:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596278#M171663</guid>
      <dc:creator>thewan</dc:creator>
      <dc:date>2019-10-14T14:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate user input for a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596280#M171664</link>
      <description>&lt;P&gt;Because the parameter name is the same as the external macro variable you cannot see the value of the external maaro variable when the macro is running.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you make the parameter name (the name of the LOCAL macro variable) different than the name of the other macro variable then you can reference both inside the macro.&amp;nbsp; Then you can add logic to use that value if the caller didn't provide a value for the parameter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro example(local_num);
%if 0=%length(&amp;amp;local_num) and %symexist(num) %then 
  %let local_num=&amp;amp;num;
;

data new;
   set time;
   array utime (&amp;amp;local_num) time1 - time&amp;amp;local_num;
      do i = 1 to &amp;amp;local_num;
         utime[i] = utime[i] + 1;
      end;
   drop i;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the user can pass in a value or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num=3 ;
%example(5)
%example(local_num=7)
%example()
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or they can just pass in the value of the external macro variable in the call, which will work even when the local macro variable uses the same name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%example(&amp;amp;num)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 14:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596280#M171664</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-14T14:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate user input for a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596281#M171665</link>
      <description>&lt;P&gt;Since the macro variable &amp;amp;NUM is given a value in the first line of the program, you don't need to give it a value again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%example(num=&amp;amp;num)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, in this very simple example, where &amp;amp;NUM has a value in the first line of the program, you don't even have to use the NUM= argument when you define or call the macro. This should work as well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro example;
...
%mend;

%example&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 14:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596281#M171665</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-14T14:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate user input for a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596286#M171666</link>
      <description>&lt;P&gt;Note I did create a SAS macro that will find the value for an existing macro variable that is currently hidden by the existence of another macro variable of the same name in a more local scope.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/symget.sas" target="_blank" rel="noopener"&gt;https://github.com/sasutils/macros/blob/master/symget.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;If you want to use it as is then you will also need the %QLIST() macro from the same site.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/symget.sas" target="_blank" rel="noopener"&gt;https://github.com/sasutils/macros/blob/master/qlist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably don't need it to fix your program, but it is a fun toy to play with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro example(num);
%if 0=%length(&amp;amp;num) %then %let num=%symget(num);
....
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 14:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596286#M171666</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-14T14:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: Consolidate user input for a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596298#M171669</link>
      <description>&lt;P&gt;Thanks for the replies, Tom and Paige.&lt;/P&gt;&lt;P&gt;I didn't think of using &amp;amp;num as the macro input, but that seems to work.&lt;/P&gt;&lt;P&gt;Thanks for links, Tom. You're right, it's more than what I need for this macro, but the step-by-step explanations are helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 15:14:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Consolidate-user-input-for-a-macro/m-p/596298#M171669</guid>
      <dc:creator>thewan</dc:creator>
      <dc:date>2019-10-14T15:14:54Z</dc:date>
    </item>
  </channel>
</rss>

