<?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: Assigning macro variables to input parameters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641364#M191138</link>
    <description>&lt;P&gt;Sorry if I was not clear enough for you. I already got some replies with the solution. Very much appreciated your input.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Apr 2020 15:29:30 GMT</pubDate>
    <dc:creator>alexgonzalez</dc:creator>
    <dc:date>2020-04-20T15:29:30Z</dc:date>
    <item>
      <title>Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641333#M191123</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;I want to create certain number of macro variables within a macro. Those macro variables are used as input variables in a macro in a unique parameter. For example:&lt;/P&gt;&lt;P&gt;%macro example (vars);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data t;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input mylib.mydata (keep = &amp;amp;vars);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;%mend;&lt;/P&gt;&lt;P&gt;%example (vars= a b c);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the number of variables I pass into the macro each time can be different, I want to assign separate values to each one (e.g. a, b, and c) to perform certain calculations. I create a temporary dataset z in the macro as follow:&lt;/P&gt;&lt;P&gt;Var &amp;nbsp;&amp;nbsp;&amp;nbsp;value&lt;/P&gt;&lt;P&gt;a &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20&lt;/P&gt;&lt;P&gt;b &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;17&lt;/P&gt;&lt;P&gt;c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&lt;/P&gt;&lt;P&gt;I’m not sure how to create three (or #) macro variables from the previous output. I’m trying something like shown below, but it is not working:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; set z;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i=1 to &amp;amp;numb;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/*previously determined in the macro (in this case 3)*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; %if _n_ = i &amp;nbsp;%then &amp;nbsp;call symputx &amp;nbsp;( “value&amp;amp;i” , value);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;Could you please advice?&lt;/P&gt;&lt;P&gt;Thanks a lot,&lt;/P&gt;&lt;P&gt;AG.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641333#M191123</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T15:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641348#M191126</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/152501"&gt;@alexgonzalez&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please specify the expected result?&lt;/P&gt;
&lt;P&gt;Do you mean that you want to create a macrovariable "a" that has the value 20, etc. ?&lt;/P&gt;
&lt;P&gt;In this case, no need to have a do loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set z;
    call symputx (var, value);
    /*or*/
    call symputx ("value"||left(_n_),value);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 87          %put &amp;amp;a. &amp;amp;b. &amp;amp;c.;
 20 17 30
 88          %put &amp;amp;value1. &amp;amp;value2. &amp;amp;value3.;
 20 17 30&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641348#M191126</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-04-20T15:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641350#M191128</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need a macro loop to create multiple macro variables with CALL SYMPUTX.&amp;nbsp; Since the DATA step is a loop, who can use that, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup ;
  input var $1. value ;
  cards ;
a 20
b 17
c 30
;

data _null_ ;
  set lookup ;
  call symputx(cats("Value",_n_),value) ;
run ;

%put &amp;amp;=Value1 &amp;amp;=Value2 &amp;amp;=Value3 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will create the three macro variable value1-value3:&lt;/P&gt;
&lt;PRE&gt;400  %put &amp;amp;=Value1 &amp;amp;=Value2 &amp;amp;=Value3 ;
VALUE1=20 VALUE2=17 VALUE3=30
&lt;/PRE&gt;
&lt;P&gt;And there are other ways to build a similar "array" of macro variables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, I think your idea of creating a dataset Z with name-value pairs was a good one.&amp;nbsp; Instead of reading that dataset to create macro variables and using those macro variables to pass values to your macro, it might be cleaner to simply pass name of the dataset Z to your macro.&amp;nbsp; That often allows you to avoid the mess of going from values stored in a SAS dataset, to values stored as text in macro variables, and then back to values stored in SAS datasets.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641350#M191128</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-04-20T15:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641351#M191129</link>
      <description>&lt;P&gt;I am not understanding your question at all. Your example macro has ONE parameter. It accepts a list of VARIABLE names. How do &lt;STRONG&gt;macro&lt;/STRONG&gt; variables come into the problem?&amp;nbsp; Are they the input or the output? Where?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you saying you want to create macro variables that have the same names as the actual variables? Just use CALL SYMPUTX(). You can force the macro variables into the global symbol table using the third parameter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variables are all of the same type you can use an array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array x &amp;amp;vars;
do i=1 to dim(x);
  call symputx(vname(x[i]),x[i],'g');
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If not then you need to make a macro loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;vars),%str( ));
call symputx("%scan(&amp;amp;var,&amp;amp;i,%str( ))",%scan(&amp;amp;var,&amp;amp;i,%str( )),'g');
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641351#M191129</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-20T15:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641354#M191131</link>
      <description>&lt;P&gt;please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data z;
input Var$ value;
cards;
a 20
b 17
c 30
;


   data _null_;
                 set z;
                   do i=1 to 3;        /*previously determined in the macro (in this case 3)*/
                                 if _n_ = i  then  call symputx  (var , value);
                   end;
              run;


%put &amp;amp;a &amp;amp;b &amp;amp;c;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641354#M191131</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-20T15:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641355#M191132</link>
      <description>&lt;P&gt;That's exactly what I wanted. Assign values to each one of the variables.&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:27:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641355#M191132</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T15:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641360#M191134</link>
      <description>&lt;P&gt;Perfect, thank you very much.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641360#M191134</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T15:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641362#M191136</link>
      <description>You're welcome!</description>
      <pubDate>Mon, 20 Apr 2020 15:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641362#M191136</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-04-20T15:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning macro variables to input parameters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641364#M191138</link>
      <description>&lt;P&gt;Sorry if I was not clear enough for you. I already got some replies with the solution. Very much appreciated your input.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 15:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641364#M191138</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T15:29:30Z</dc:date>
    </item>
  </channel>
</rss>

