<?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: Loop through variables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742047#M80568</link>
    <description>&lt;P&gt;Easy to do with arrays:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have
    array cnts (*) cnt1 - cnt11;
    array ocnts (*) $ 3 ocnt1 - ocnt11; 
	do i=1 to dim(cnts);
	  ocnts (i). = left(put(cnts(i), 3.));
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 18 May 2021 03:08:54 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2021-05-18T03:08:54Z</dc:date>
    <item>
      <title>Loop through variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742038#M80566</link>
      <description>&lt;P&gt;I have variables &lt;STRONG&gt;cnt1&lt;/STRONG&gt; through &lt;STRONG&gt;cnt11&lt;/STRONG&gt;. They are numeric (containing values between 1 and 15), but I need to make these variables character. If there is someway to do this without a loop, that would be great; but it seems this may only work with a loop. The following is what I want to work, but I realize this is not how you specify how you iterate through variable names with different numeric suffixes. Does anyone know how to do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
	set have

	do i=1 to &amp;amp;max_n_off;
	  ocnt&amp;amp;i. = put(cnt&amp;amp;i., 3.);
	end;

run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 May 2021 00:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742038#M80566</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-05-18T00:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742047#M80568</link>
      <description>&lt;P&gt;Easy to do with arrays:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have
    array cnts (*) cnt1 - cnt11;
    array ocnts (*) $ 3 ocnt1 - ocnt11; 
	do i=1 to dim(cnts);
	  ocnts (i). = left(put(cnts(i), 3.));
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 May 2021 03:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742047#M80568</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-05-18T03:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742053#M80569</link>
      <description>&lt;P&gt;I would go wiht&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;'s solution, but just to complete the macro approach you were considering:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doloop(upper_limit=);
data want;
  set have;
  %do i=1 %to &amp;amp;upper_limit;
    c&amp;amp;i=put(x&amp;amp;i,4.);
  %end;
run;
%mend doloop;
options mprint;
%doloop(upper_limit=3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Way too much typing.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 03:35:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742053#M80569</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-18T03:35:50Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742110#M80570</link>
      <description>&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;that ARRAYs are the way to do this, but to explain further, you cannot mix and match macro language and data step language the way you have done in your original code. In your original code, you refer to macro variable &lt;FONT face="courier new,courier"&gt;&amp;amp;i&lt;/FONT&gt; but you never define macro variable &lt;FONT face="courier new,courier"&gt;&amp;amp;i&lt;/FONT&gt; or give it a value. In addition, macro variables are not data set variables, and &lt;EM&gt;vice versa&lt;/EM&gt;. Just because you have a data set variable named &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; does not mean it has any relationship to a macro variable &lt;FONT face="courier new,courier"&gt;&amp;amp;i&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 11:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742110#M80570</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-18T11:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742162#M80571</link>
      <description>&lt;P&gt;You use ARRAY statement to allow you to loop over a list of variables.&lt;/P&gt;
&lt;P&gt;You will need to make new variables since you cannot change the existing variables.&lt;/P&gt;
&lt;P&gt;If you want to re-use the names you can use RENAME to change the names back.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  array old cnt1-cnt15 ;
  array new $3 ocnt1-ocnt15;
  do index=1 to dim(old);
    new[index] = put(old[index],3.);
  end;
  drop index;
  rename cnt1-cnt15=charcnt1-charcnt15 ocnt1-ocnt15=cnt1-cnt15 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you really want your new character values to have leading spaces if the number is less than 100?&lt;/P&gt;
&lt;P&gt;If not you can use either the Z3. format to include leading zeros instead.&lt;/P&gt;
&lt;P&gt;Or use the -L format modifier to left align the values.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;    new[index] = put(old[index],3.-L);&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 13:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Loop-through-variables/m-p/742162#M80571</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-18T13:57:22Z</dc:date>
    </item>
  </channel>
</rss>

