<?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 would I write this loop with proc sql to select car1, car2, car3 up to car10 from table A? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653092#M196150</link>
    <description>&lt;P&gt;Pretty much as soon as you say "loop" you are out of the SQL world.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One data set approach would be:&lt;/P&gt;
&lt;PRE&gt;data work.table;
   set work.dataset;
   array c car1-car10;
   do i= 1 to dim(c);
      if c[i] in (&amp;amp;IN_SCOPE_CARS.) then do;
         output;
         leave;
      end;
   end;
   keep car1-car10;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;where you have a small code advantage that you can use arrays to loop over variable values and shorthand lists to reference similarly named (in this instance anyway) variables.&lt;/P&gt;
&lt;P&gt;The only tricky parts are inside the loop where we have an explicit output to send the current values to the output data set and the special instruction LEAVE which in this case will stop the loop as soon as one match is found. I would go this way if you start having to deal with more Car variables just for the ability to use lists. Change the car10 in two places to car100 and you can deal with 100 variables and values. SQL you get to list another 90 variables in the Select and another 90 OR clauses in the Where coding.&lt;/P&gt;</description>
    <pubDate>Thu, 04 Jun 2020 07:36:34 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-04T07:36:34Z</dc:date>
    <item>
      <title>How would I write this loop with proc sql to select car1, car2, car3 up to car10 from table A?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653045#M196127</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;First time posting!&amp;nbsp; I have some sql code where I Select multiple columns with the same name except the final digit in them.&amp;nbsp; I'd like to clean up the code and cannot figure out the loop to write for this. The &amp;amp;IN_SCOPE_CARS is a list of cars that I want to only include in the table.&amp;nbsp; I also searched and could not seem to find a solution for when all the columns names are what is being looped through and the table was staying the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;CREATE TABLE WORK.TABLE AS&lt;/P&gt;&lt;P&gt;SELECT&lt;/P&gt;&lt;P&gt;&amp;nbsp;CAR1&lt;/P&gt;&lt;P&gt;,CAR2&lt;/P&gt;&lt;P&gt;,CAR3&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;,CAR10&lt;/P&gt;&lt;P&gt;FROM WORK.DATASET&lt;/P&gt;&lt;P&gt;WHERE&lt;/P&gt;&lt;P&gt;CAR1 IN (&amp;amp;IN_SCOPE_CARS.) OR&lt;/P&gt;&lt;P&gt;CAR2 IN (&amp;amp;IN_SCOPE_CARS.) OR&lt;/P&gt;&lt;P&gt;CAR3 IN (&amp;amp;IN_SCOPE_CARS.) OR&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;CAR10 IN (&amp;amp;IN_SCOPE_CARS.)&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jun 2020 03:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653045#M196127</guid>
      <dc:creator>NRichmond</dc:creator>
      <dc:date>2020-06-04T03:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: How would I write this loop with proc sql to select car1, car2, car3 up to car10 from table A?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653060#M196135</link>
      <description>&lt;P&gt;I don't see how you could make this code much better.&lt;/P&gt;
&lt;P&gt;You only have 20 lines of code, and any attempt at some clever loop will not make the code much more compact, while it will certainly make the code much harder to read.&lt;/P&gt;
&lt;P&gt;Sometimes no action is the best action &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jun 2020 04:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653060#M196135</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-06-04T04:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: How would I write this loop with proc sql to select car1, car2, car3 up to car10 from table A?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653092#M196150</link>
      <description>&lt;P&gt;Pretty much as soon as you say "loop" you are out of the SQL world.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One data set approach would be:&lt;/P&gt;
&lt;PRE&gt;data work.table;
   set work.dataset;
   array c car1-car10;
   do i= 1 to dim(c);
      if c[i] in (&amp;amp;IN_SCOPE_CARS.) then do;
         output;
         leave;
      end;
   end;
   keep car1-car10;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;where you have a small code advantage that you can use arrays to loop over variable values and shorthand lists to reference similarly named (in this instance anyway) variables.&lt;/P&gt;
&lt;P&gt;The only tricky parts are inside the loop where we have an explicit output to send the current values to the output data set and the special instruction LEAVE which in this case will stop the loop as soon as one match is found. I would go this way if you start having to deal with more Car variables just for the ability to use lists. Change the car10 in two places to car100 and you can deal with 100 variables and values. SQL you get to list another 90 variables in the Select and another 90 OR clauses in the Where coding.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jun 2020 07:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653092#M196150</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-04T07:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: How would I write this loop with proc sql to select car1, car2, car3 up to car10 from table A?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653109#M196156</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162552"&gt;@NRichmond&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;The first part of the SELECT statement could be abbreviated by using a variable list in a KEEP= dataset option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * from work.dataset(keep=car1-car10);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or (under certain conditions)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select * from dataset(keep=car:);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would also consider transposing (relevant parts of)&amp;nbsp;&lt;FONT face="courier new,courier"&gt;work.dataset&lt;/FONT&gt; from wide to long (with a single variable &lt;FONT face="courier new,courier"&gt;car&lt;/FONT&gt;), which would allow for easy subsetting in PROC SQL like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;having max(car in (&amp;amp;in_scope_cars));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;after a suitable GROUP BY clause.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jun 2020 08:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-would-I-write-this-loop-with-proc-sql-to-select-car1-car2/m-p/653109#M196156</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-06-04T08:35:45Z</dc:date>
    </item>
  </channel>
</rss>

