<?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: How to call macro variable within a looping code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945017#M370260</link>
    <description>&lt;P&gt;This is a classic SAS macro issue of timing. There are two main things you need to change in your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_055/lefunctionsref/p12zqzvwx4dv6kn1p9crijxswolk.htm" target="_self"&gt;PUTN Function&lt;/A&gt; instead of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_055/lefunctionsref/n0mlfb88dkhbmun1x08qbh5xbs7e.htm" target="_self"&gt;PUT Function&lt;/A&gt;. The PUTN Function e&lt;SPAN&gt;nables you to specify a numeric format at run time. The Put Function does so at compile time, i.e. before the loop executes.&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n00cqfgax81a11n1oww7hwno4aae.htm" target="_self"&gt;Symget Function&lt;/A&gt; in the Data Step to retrieve the value of the macro variable during DATA step execution (not when it compiles).&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I changed the input values a bit, to make the result clearer. Feel free to ask.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data df1;
   a = .;     b=1.235; c=1.234; output;
   a = 1.234; b=.;     c=1.234; output;
   a = 1.234; b=1.234; c=.;     output;
run; 

%let _width1=5.1;
%let _width2=5.2;
%let _width3=5.3;

data want ;
   set df1;
   array _invars{3} a b c;
   array _inval{3} $16 ;

   do i = 1 to 3;
      f = symget(cats('_width', i));
      _inval{i}=putn(_invars{i},  f);
   end;     
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;a      b      c      _inval1  _inval2  _inval3  i  f
.      1.235  1.234  .        1.24     1.234    4  5.3
1.234  .      1.234  1.2      .        1.234    4  5.3
1.234  1.234  .      1.2      1.23     .        4  5.3&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 24 Sep 2024 10:59:47 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2024-09-24T10:59:47Z</dc:date>
    <item>
      <title>How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945008#M370257</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to know whether it is possible to call marco variables with a looping code. Please see example below:&lt;/P&gt;
&lt;P&gt;when i=1, I would like to get:&amp;nbsp; _inval{1}=put( _invars{1},&amp;amp;_width1).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Current codes won't work.&lt;/P&gt;
&lt;PRE&gt;  data df1;
	a = .; b=1; c=1; output;
	a = 1; b=.; c=1; output;
	a = 1; b=1; c=.; output;
run; 

%let _width1=3.1;
%let _width2=3.2;
%let _width3=3.3;
data want ;
set df1;
  array _invars{3} a b c;
  array _inval{3} $16 ;

  do i=1 to 3.;
  	 _inval{i}=put( _invars{i},&amp;amp;_widthi)	; /*I want to use &amp;amp;_width(i) where i change with i in looping setting */
  end;		
 run ;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Sep 2024 09:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945008#M370257</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-09-24T09:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945017#M370260</link>
      <description>&lt;P&gt;This is a classic SAS macro issue of timing. There are two main things you need to change in your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_055/lefunctionsref/p12zqzvwx4dv6kn1p9crijxswolk.htm" target="_self"&gt;PUTN Function&lt;/A&gt; instead of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_055/lefunctionsref/n0mlfb88dkhbmun1x08qbh5xbs7e.htm" target="_self"&gt;PUT Function&lt;/A&gt;. The PUTN Function e&lt;SPAN&gt;nables you to specify a numeric format at run time. The Put Function does so at compile time, i.e. before the loop executes.&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n00cqfgax81a11n1oww7hwno4aae.htm" target="_self"&gt;Symget Function&lt;/A&gt; in the Data Step to retrieve the value of the macro variable during DATA step execution (not when it compiles).&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I changed the input values a bit, to make the result clearer. Feel free to ask.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data df1;
   a = .;     b=1.235; c=1.234; output;
   a = 1.234; b=.;     c=1.234; output;
   a = 1.234; b=1.234; c=.;     output;
run; 

%let _width1=5.1;
%let _width2=5.2;
%let _width3=5.3;

data want ;
   set df1;
   array _invars{3} a b c;
   array _inval{3} $16 ;

   do i = 1 to 3;
      f = symget(cats('_width', i));
      _inval{i}=putn(_invars{i},  f);
   end;     
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;a      b      c      _inval1  _inval2  _inval3  i  f
.      1.235  1.234  .        1.24     1.234    4  5.3
1.234  .      1.234  1.2      .        1.234    4  5.3
1.234  1.234  .      1.2      1.23     .        4  5.3&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 10:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945017#M370260</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-09-24T10:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945020#M370262</link>
      <description>&lt;P&gt;I'd say, with OP setup there is no need for macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The PUTN which&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;mentioned is all is needed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data df1;
   a = .;     b=1.235; c=1.234; output;
   a = 1.234; b=.;     c=1.234; output;
   a = 1.234; b=1.234; c=.;     output;
run; 

data want ;
   set df1;
   array _invars{3} a b c;
   array _inval{3} $16 ;

   do i = 1 to 3;
      _inval{i}=putn(_invars{i},  cats("5.",i)); /* build "5.i" text */
   end;     

   put _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 11:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945020#M370262</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-09-24T11:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945033#M370264</link>
      <description>&lt;P&gt;No need for macro variables.&amp;nbsp; Just put the formats into another array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set df1;
  array _invars a b c;
  array _inval[3] $16 ;
  array _formats[3] $8 _temporary_ ('5.1' '5.2' '5.3');
  do i=1 to 3.;
/*I want to use &amp;amp;_width(i) where i change with i in looping setting */
    _inval[i]=putN( _invars[i],_formats[i]);
  end;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Sep 2024 12:45:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945033#M370264</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-24T12:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945043#M370267</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;Thanks so much!&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;Can we do&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;putn(_invars{i},  &amp;amp;_width1.)&lt;/LI-CODE&gt;
&lt;P&gt;directly? I got error message for "expecting a format" &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One question might be very silly. How can I tell when an 'i' will change base on the looping i setup?&amp;nbsp; I was hoping the i in &amp;amp;widthi will change when I do looping. Could you guide me on this? Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 13:49:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945043#M370267</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-09-24T13:49:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945048#M370268</link>
      <description>&lt;P&gt;You have an error because PUTN() function (go to see its documentation: &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p12zqzvwx4dv6kn1p9crijxswolk.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p12zqzvwx4dv6kn1p9crijxswolk.htm&lt;/A&gt;&amp;nbsp;, unfortunatell the description there in vague)&lt;/P&gt;
&lt;P&gt;expects a string constant or a character variable/expression which provides format name.&lt;/P&gt;
&lt;P&gt;For example if&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%let _width1=best32.20;&lt;/LI-CODE&gt;
&lt;P&gt;then your code should be:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;/* option 1 */
putn(_invars{i},  "&amp;amp;_width1.")

/* option 2 */
putn(_invars{i},  symget("_width1"))&lt;/LI-CODE&gt;
&lt;P&gt;Basically, think about it that you want to have:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;putn(variable, "format-name.")

/* or */
xxx="format-name.";
putn(variable, xxx)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 14:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945048#M370268</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-09-24T14:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to call macro variable within a looping code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945068#M370275</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;Thanks so much!&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;Can we do&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;putn(_invars{i},  &amp;amp;_width1.)&lt;/LI-CODE&gt;
&lt;P&gt;directly? I got error message for "expecting a format" &lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One question might be very silly. How can I tell when an 'i' will change base on the looping i setup?&amp;nbsp; I was hoping the i in &amp;amp;widthi will change when I do looping. Could you guide me on this? Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Remember that the macro processor is a PRE processor.&amp;nbsp; The results after the macro processor has finished needs to be valid SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what value did _WIDTH1 have?&amp;nbsp; If it was just 5.3 (without any quotes) then your statement becomes something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;putn(x,5.3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which is not valid SAS code because the PUTN() function wants a character value for the second argument (and a numeric value for the first. If you are formatting a character string use the PUTC() function instead.).&amp;nbsp; In this context the string 5.3 is a number, not a character string.&amp;nbsp; It might be interpreted as a format specification in locations where such was expected, but that is not what PUTN() expects.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So either add the quotes when referencing the macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;putn(x,"&amp;amp;_width1")&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or make sure the quotes are in the macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let _width1="5.3";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that the resolved SAS code is something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;putn(x,"5.3")&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 15:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-call-macro-variable-within-a-looping-code/m-p/945068#M370275</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-24T15:34:00Z</dc:date>
    </item>
  </channel>
</rss>

