<?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: Macro loop through selected columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678874#M204978</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let vars = a b c;

data want;
set have;
array have {*} &amp;amp;vars.;
array ret {%sysfunc(countw(&amp;amp;vars.))} _temporary_;
do i = 1 to dim(have);
  if have{i} ne .
  then ret{i} = have{i};
  else have{i} = ret{i};
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet. If it works, all you have to do is to populate the macro variable.&lt;/P&gt;</description>
    <pubDate>Mon, 24 Aug 2020 13:45:52 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-08-24T13:45:52Z</dc:date>
    <item>
      <title>Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678860#M204970</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Wanna ask any efficient ways to do same operations on multiple columns?&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;I have a macro named "%fun" and column names of&lt;BR /&gt;A1 A2 A3 A4 A5 A6 B1 B2 B3 B4 B5&lt;BR /&gt;&lt;BR /&gt;So i wish to loop the macro %fun across columns A4 to B4, any ideas how to code this? (ps. In real dataset, I have about 300 columns)</description>
      <pubDate>Mon, 24 Aug 2020 12:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678860#M204970</guid>
      <dc:creator>someone_new</dc:creator>
      <dc:date>2020-08-24T12:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678862#M204972</link>
      <description>&lt;P&gt;What does the macro do? It might be that you don't have to call the macro repeatedly, but can use a data step do loop over an array, or similar.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 12:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678862#M204972</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-24T12:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678863#M204973</link>
      <description>Basically the macro %fun retain a previous value and place into missing value or cells</description>
      <pubDate>Mon, 24 Aug 2020 12:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678863#M204973</guid>
      <dc:creator>someone_new</dc:creator>
      <dc:date>2020-08-24T12:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678866#M204974</link>
      <description>&lt;P&gt;That can be done in a single data step, using an array for the variables and a temporary array of same size for the retained values.&lt;/P&gt;
&lt;P&gt;All you need to create first is a macro variable with the variable names, for the array definitions; the size of the temporary array can be determined from that with %sysfunc(countw()).&lt;/P&gt;
&lt;P&gt;Show the code you run for a single variable, and we'll show you how to expand it.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 12:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678866#M204974</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-24T12:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678867#M204975</link>
      <description>%macro fun(variable) ;&lt;BR /&gt;retain _&amp;amp;variable;&lt;BR /&gt;if &amp;amp;variable ^=. then _&amp;amp;variable = &amp;amp;variable;&lt;BR /&gt;else &amp;amp;variable = _&amp;amp;variable;&lt;BR /&gt;drop _&amp;amp;variable;&lt;BR /&gt;%mend;</description>
      <pubDate>Mon, 24 Aug 2020 13:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678867#M204975</guid>
      <dc:creator>someone_new</dc:creator>
      <dc:date>2020-08-24T13:05:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678874#M204978</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let vars = a b c;

data want;
set have;
array have {*} &amp;amp;vars.;
array ret {%sysfunc(countw(&amp;amp;vars.))} _temporary_;
do i = 1 to dim(have);
  if have{i} ne .
  then ret{i} = have{i};
  else have{i} = ret{i};
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet. If it works, all you have to do is to populate the macro variable.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 13:45:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678874#M204978</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-24T13:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678880#M204979</link>
      <description>How about range of columns instead of %let vars method?</description>
      <pubDate>Mon, 24 Aug 2020 14:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678880#M204979</guid>
      <dc:creator>someone_new</dc:creator>
      <dc:date>2020-08-24T14:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678895#M204983</link>
      <description>Yours solution works perfectly thank you so muchhhhhh you save my day thank you!!!</description>
      <pubDate>Mon, 24 Aug 2020 14:57:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678895#M204983</guid>
      <dc:creator>someone_new</dc:creator>
      <dc:date>2020-08-24T14:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678897#M204985</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300958"&gt;@someone_new&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How about range of columns instead of %let vars method?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want to use a variable list (var1-var10 for example) instead of naming each variable then you need to modify the data step to just make the temporary array large enough for any possible real use.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let vars = a b var1-var20 firstvar-numeric-lastvar;
...
array have {*} &amp;amp;vars.;
array ret {1000} _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Aug 2020 15:03:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678897#M204985</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-24T15:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678930#M205007</link>
      <description>&lt;P&gt;You need to know the number of variables before you enter the data step, to correctly define the temporary array. So you will need to determine the list of variables, anyway.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300958"&gt;@someone_new&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How about range of columns instead of %let vars method?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 16:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678930#M205007</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-24T16:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678937#M205012</link>
      <description>You just need to know an upper bound on the number the number of variables. The size of the temporary array does not matter as long as it is at least as large as the actual array.</description>
      <pubDate>Mon, 24 Aug 2020 17:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678937#M205012</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-24T17:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678938#M205013</link>
      <description>&lt;P&gt;My inner old-school coder simply does not want to waste memory &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 17:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/678938#M205013</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-24T17:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro loop through selected columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/681628#M206192</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; i have new challenging problem mind help me solve it? I have posted in my profile</description>
      <pubDate>Fri, 04 Sep 2020 13:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-loop-through-selected-columns/m-p/681628#M206192</guid>
      <dc:creator>someone_new</dc:creator>
      <dc:date>2020-09-04T13:21:27Z</dc:date>
    </item>
  </channel>
</rss>

