<?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: Looping through variables when importing data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745388#M233633</link>
    <description>&lt;P&gt;Another way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/(~123|~13a|~2a1)/', '~'||catx('~',of ATTRIB1-ATTRIB20) ) then output;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Jun 2021 04:20:45 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-06-03T04:20:45Z</dc:date>
    <item>
      <title>Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745382#M233629</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;I have a SAS dataset called &lt;STRONG&gt;sasdata1&lt;/STRONG&gt; which has 40 variables with 20 of them named "&lt;STRONG&gt;attrib1&lt;/STRONG&gt;" to "&lt;STRONG&gt;attrib20&lt;/STRONG&gt;". There is also a variable called &lt;STRONG&gt;numattrib&lt;/STRONG&gt; that stores the number of non-empty attributes starting from attrib1 (Obviously the value of numattrib could be different for different records). I want to create a sub-dataset called &lt;STRONG&gt;sasdata2&lt;/STRONG&gt; with records meeting the condition that any of attrib1-attrib20 starts with either "123" or "13a" or "2a1". Here is my SAS code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA sasdata2;&lt;BR /&gt;SET sasdata1;&lt;BR /&gt;if&lt;/P&gt;&lt;P&gt;Substr(&lt;STRONG&gt;attrib1&lt;/STRONG&gt;,1,3) in ('123','13a','2a1') or&lt;BR /&gt;Substr(&lt;STRONG&gt;attrib2&lt;/STRONG&gt;,1,3) in ('123','13a','2a1') or&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;Substr(&lt;STRONG&gt;attrib20&lt;/STRONG&gt;,1,3) in ('123','13a','2a1') or&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My first question is whether it is possible to consolidate these 20 lines of code after the key word if. Second question is whether we can take advantage of the variable&amp;nbsp;&lt;STRONG&gt;numattrib&lt;/STRONG&gt; to design a more efficient import process.&lt;/P&gt;&lt;P&gt;Thank you very much in advance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 03:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745382#M233629</guid>
      <dc:creator>sasfyj</dc:creator>
      <dc:date>2021-06-03T03:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745386#M233631</link>
      <description>please upload a sample dataset for the test</description>
      <pubDate>Thu, 03 Jun 2021 03:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745386#M233631</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2021-06-03T03:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745387#M233632</link>
      <description>&lt;P&gt;Would this work for you?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if index('|'||catx('|',of ATTRIB1-ATTRIB20), "|123") 
 | index('|'||catx('|',of ATTRIB1-ATTRIB20), "|13a") 
 | index('|'||catx('|',of ATTRIB1-ATTRIB20), "|2a1") then output;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 04:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745387#M233632</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-06-03T04:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745388#M233633</link>
      <description>&lt;P&gt;Another way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/(~123|~13a|~2a1)/', '~'||catx('~',of ATTRIB1-ATTRIB20) ) then output;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 04:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745388#M233633</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-06-03T04:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745407#M233640</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/53873"&gt;@Chris&lt;/a&gt;&amp;nbsp;you need a starting anchor in the regex so IMHO catx is not suitable here&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;maybe something like this:&lt;/P&gt;
&lt;PRE&gt;ARRAY attribs ATTRIB1-ATTRIB20;&lt;BR /&gt;DO i=1 to dim(ATTRIBS);&lt;BR /&gt;if not prxmatch('/^(123|13a|2a1)/',ATTRIBS(i)) then leave;&lt;BR /&gt;if i eq 20 then output;&lt;BR /&gt;END;&lt;BR /&gt;drop i;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 08:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745407#M233640</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2021-06-03T08:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745409#M233642</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/384254"&gt;@sasfyj&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To utilize &lt;FONT face="courier new,courier"&gt;numattrib&lt;/FONT&gt; use an iterative DO loop "... TO &lt;FONT face="courier new,courier"&gt;numattrib&lt;/FONT&gt;."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sasdata2(drop=i);
set sasdata1;
array a[*] attrib1-attrib20;
do i=1 to numattrib until(a[i] in: ('123','13a','2a1'));
end;
if i&amp;lt;=numattrib;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jun 2021 08:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745409#M233642</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-03T08:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745428#M233648</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;&amp;nbsp;you need a starting anchor&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;You mean the tilde I add before catx() ?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 10:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745428#M233648</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-06-03T10:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745654#M233780</link>
      <description>&lt;P&gt;Thanks. It works as well.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 23:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745654#M233780</guid>
      <dc:creator>sasfyj</dc:creator>
      <dc:date>2021-06-03T23:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745655#M233781</link>
      <description>&lt;P&gt;Thank you. It works properly.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 23:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745655#M233781</guid>
      <dc:creator>sasfyj</dc:creator>
      <dc:date>2021-06-03T23:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745656#M233782</link>
      <description>&lt;P&gt;Thanks for your help. I think we should remove 'Not' and change eq to le&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 23:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745656#M233782</guid>
      <dc:creator>sasfyj</dc:creator>
      <dc:date>2021-06-03T23:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through variables when importing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745701#M233803</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;your solution is correct, my bad.&lt;/P&gt;
&lt;P&gt;I meant that all the variables had to meet the prefix defined, but one is enough according to the requirements.&lt;/P&gt;
&lt;P&gt;This is definitely haute couture of programming as I like it.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 08:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-variables-when-importing-data/m-p/745701#M233803</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2021-06-04T08:27:55Z</dc:date>
    </item>
  </channel>
</rss>

