<?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: Loops from first to last in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443849#M111082</link>
    <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't think that will work as I have 100's of subtechs that I need to automate their listing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be like&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; tech1set as
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; dataset2
&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; subtech &lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; ABCD, ABCE, ABCF, DCAB, ...  &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; table1&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There's no way I would hand-type out those SubTechs, and the list of SubTechs is expanding, which is why I need to pull the most up to date list from the first dataset that gets updated with all active subtechs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I just literally "select subtech from tabel1" then it wouldn't subset the data between the major tech groups.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Mar 2018 18:04:57 GMT</pubDate>
    <dc:creator>abak</dc:creator>
    <dc:date>2018-03-08T18:04:57Z</dc:date>
    <item>
      <title>Loops from first to last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443842#M111079</link>
      <description>&lt;P&gt;Hello. Noob here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tech&amp;nbsp; &amp;nbsp; &amp;nbsp;SubTech&lt;/P&gt;&lt;P&gt;T1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AB11&lt;/P&gt;&lt;P&gt;T1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;JDE1&lt;/P&gt;&lt;P&gt;T1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LEIO&lt;/P&gt;&lt;P&gt;T2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AJNC&lt;/P&gt;&lt;P&gt;T2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;JUN1&lt;/P&gt;&lt;P&gt;T2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PLNL&lt;/P&gt;&lt;P&gt;T3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TNEVE&lt;/P&gt;&lt;P&gt;T3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NED0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to use two loops, the outside to loop through the three technologies, and the inner one for each subtech within, to create macro variables something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;subs1 = "JDE1, LEI0"&lt;/P&gt;&lt;P&gt;subs2 = "AJNC, JUN1, PLNL"&lt;/P&gt;&lt;P&gt;subs3="TNEVE, NED0"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;via subs&amp;amp;i = (correct command here)&lt;/P&gt;&lt;P&gt;Something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set techs;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; if first.Tech = 1 then do until last.Tech = 1&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; subs&amp;amp;i = ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;second loop over j in here&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;subs&amp;amp;i = subs&amp;amp;i.||&amp;lt;AppropriateSubTech&amp;gt;; *(trying to paste together a list here like&amp;nbsp;&lt;SPAN&gt;"JDE1, LEI0")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;run;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Two questions:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) How do I define the inside loop?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) Also, I feel like the outside loop will terminate one too early if I use last.Tech = 1 because I want to include that term in the loop as well. How do I appropriately define the index for the outer loop?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 17:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443842#M111079</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-03-08T17:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loops from first to last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443844#M111080</link>
      <description>&lt;P&gt;The point of this exercise is to subset another dataset that doesn't have the Tech column but has the SubTech column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eg,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset2 is like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SubTech Data&lt;/P&gt;&lt;P&gt;AB11&amp;nbsp; &amp;nbsp; &amp;nbsp; 0.102&lt;/P&gt;&lt;P&gt;AB11&amp;nbsp; &amp;nbsp; &amp;nbsp; -1.22&lt;/P&gt;&lt;P&gt;LEI0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.73&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Tech1Set;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;set dataset2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if SubTech IN (&amp;amp;subs1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 17:38:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443844#M111080</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-03-08T17:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: Loops from first to last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443848#M111081</link>
      <description>&lt;P&gt;Change your process:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc sql;
create table tech1set as
select *
from dataset2
where subtech in (select subtech from table1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Mar 2018 17:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443848#M111081</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-08T17:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: Loops from first to last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443849#M111082</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't think that will work as I have 100's of subtechs that I need to automate their listing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be like&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; tech1set as
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; dataset2
&lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt; subtech &lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; ABCD, ABCE, ABCF, DCAB, ...  &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; table1&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There's no way I would hand-type out those SubTechs, and the list of SubTechs is expanding, which is why I need to pull the most up to date list from the first dataset that gets updated with all active subtechs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I just literally "select subtech from tabel1" then it wouldn't subset the data between the major tech groups.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 18:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443849#M111082</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-03-08T18:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loops from first to last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443850#M111083</link>
      <description>&lt;P&gt;That isn't how you've shown your data. you've only shown values in a single column, not multiple variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ABCD&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; ABCE&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; ABCF&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; DCAB&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/FONT&gt; &lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; table1 -&amp;gt; you only showed two variables, Tech and SubTech, where do variables ABCD and ABCE come from? If those are values you're not understanding what I'm trying to suggest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And, Why for the first set are you missing the first entry?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;subs1 = "JDE1, LEI0" -&amp;gt; where's the first one?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So you're trying to create subsets for each tech group based on the subtech groups? You can add a WHERE to the select statement, but I don't understand&amp;nbsp;why you don't just join them and find the values you need.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;or use an approach like this.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table tech1set as
select *
from dataset2
where subtech in (select subtech from table1 where tech='T1');
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;There's some ideas here on how to create those lists but SAS doesn't loop the way you're thinking, there's already a data step loop that you need to take advantage of, as well as the BY group processing that's supported.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;See this example here:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a" target="_blank"&gt;https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can run it, and I suggest you do and see how it works. If you really, really want the macro variables at the point, its a very easy addition, it would be something like the following at each of the last.group values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.group then call symputx(group_name, variable_value);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Mar 2018 18:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443850#M111083</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-08T18:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loops from first to last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443852#M111085</link>
      <description>&lt;P&gt;I think adding the where statement will make it work. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both data sets have two columns, one has Tech and SubTech, and the other has SubTech and Data. The data sets I showed were small examples of very large 500k+ rows of information.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Missing the first entry just due to missing it when providing my example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The values ABCD, ABCE...etc, are just in the dataset. Those are the fake versions of the subtech names, and are values, not separate variables, which I assume should be the top column if they were supposed to be variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't just join them because there is some slight complexities I know how to deal with that I'm glossing over with respect to the SubTech column, which actually contains multiple techs, and therefore the two SubTech columns are not identical. I can use the where statement along with an index indicator function to get around that though.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was trying to take advantage of the internal loop structure of the data step by using a sorted data set and the first/last commands. Perhaps badly. I'll check out that link. Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 18:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops-from-first-to-last/m-p/443852#M111085</guid>
      <dc:creator>abak</dc:creator>
      <dc:date>2018-03-08T18:23:25Z</dc:date>
    </item>
  </channel>
</rss>

