<?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: Set; inside loop in SAS in two different ways in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892205#M352407</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441716"&gt;@vijaypratap0195&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can anyone explain to me the logic behind these 2 codes please (uses set in loop):&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm not entirely sure of what you are asking, but ...&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Remember, that in the absence of an explicit OUTPUT statement, there is an implicit one just prior to the RUN; statement.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;SETs inside a do loop read a succession of observations until the loop conditions are satisfied.&amp;nbsp; But only the last observation read by the loop is in memory at the end of the loop, and therefore exposed to the implicit OUTPUT.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;As a result, your first example outputs every even-numbered original observation, and the second outputs every 0mod3 obs.&amp;nbsp; Note that since the original dataset has 19 obs, in both cases the end of sashelp.class will be encountered before the end of the loop.&amp;nbsp; So the data step will immediately stop before reaching the&amp;nbsp; implicit OUTPUT and obs 19 is not in the resulting data set.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, using SETs inside a DO loop is done for purposes of aggregating a series of obs, often a having a single ID.&amp;nbsp; Once the aggregation calculations are done, often a second use of SET within a DO loop is done to reread (and explicitly OUTPUT) the same obs, but with additional group-related information.&lt;/P&gt;</description>
    <pubDate>Fri, 01 Sep 2023 02:45:19 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-09-01T02:45:19Z</dc:date>
    <item>
      <title>Set; inside loop in SAS in two different ways</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892005#M352360</link>
      <description>&lt;P&gt;Can anyone explain to me the logic behind these 2 codes please (uses set in loop):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data class1 ; 
Do I=1 to 2 ; 
Set sashelp.class; 
End;
Run;
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Code 2:&lt;/P&gt;
&lt;PRE&gt;Data class1 ; &lt;BR /&gt;Do I=1 to 3 ; &lt;BR /&gt;Set sashelp.class; &lt;BR /&gt;End;&lt;BR /&gt;Run;&lt;BR /&gt;proc print; run;&lt;/PRE&gt;
&lt;P&gt;Output code 1:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vijaypratap0195_0-1693501909081.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87383iDF564AC1CF12636D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vijaypratap0195_0-1693501909081.png" alt="vijaypratap0195_0-1693501909081.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output code 2:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vijaypratap0195_1-1693501916715.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87384i489E18013C1A2C7A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vijaypratap0195_1-1693501916715.png" alt="vijaypratap0195_1-1693501916715.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 17:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892005#M352360</guid>
      <dc:creator>vijaypratap0195</dc:creator>
      <dc:date>2023-08-31T17:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: Set; inside loop in SAS in two different ways</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892011#M352361</link>
      <description>&lt;P&gt;One way to explore this is to add PUT statements, showing the value of _N_ and other variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, try running:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class1 ; 
  do i=1 to 2 ; 
   set sashelp.class; 
   put "Inside Loop" (_N_ i Name)(=) ;
  end;
  put "Outside Loop" (_N_ i Name)(=) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Aug 2023 17:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892011#M352361</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-31T17:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Set; inside loop in SAS in two different ways</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892028#M352370</link>
      <description>&lt;P&gt;Since you read 2 (or 3) observations but output one it seems pretty obvious what it is doing.&lt;/P&gt;
&lt;P&gt;Notice how the 3rd observation in the first run matches the 2nd observation in the second run.&amp;nbsp; Confirming the mathematical equality that 2*3 is the same as 3*2.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 18:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892028#M352370</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-31T18:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Set; inside loop in SAS in two different ways</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892034#M352372</link>
      <description>&lt;P&gt;If you are trying to examine the behavior of multiple Set statements I recommend using multiple small data sets of different sizes, small number of variables, some with different names and (at least some) values unique to the data set.&lt;/P&gt;
&lt;P&gt;That way have some chance of possibly seeing which actual set contributed which record. Plus you may get an education of what happens with sets and different numbers of observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the question is about the value of I&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 18:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892034#M352372</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-31T18:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: Set; inside loop in SAS in two different ways</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892205#M352407</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441716"&gt;@vijaypratap0195&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can anyone explain to me the logic behind these 2 codes please (uses set in loop):&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm not entirely sure of what you are asking, but ...&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Remember, that in the absence of an explicit OUTPUT statement, there is an implicit one just prior to the RUN; statement.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;SETs inside a do loop read a succession of observations until the loop conditions are satisfied.&amp;nbsp; But only the last observation read by the loop is in memory at the end of the loop, and therefore exposed to the implicit OUTPUT.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;As a result, your first example outputs every even-numbered original observation, and the second outputs every 0mod3 obs.&amp;nbsp; Note that since the original dataset has 19 obs, in both cases the end of sashelp.class will be encountered before the end of the loop.&amp;nbsp; So the data step will immediately stop before reaching the&amp;nbsp; implicit OUTPUT and obs 19 is not in the resulting data set.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, using SETs inside a DO loop is done for purposes of aggregating a series of obs, often a having a single ID.&amp;nbsp; Once the aggregation calculations are done, often a second use of SET within a DO loop is done to reread (and explicitly OUTPUT) the same obs, but with additional group-related information.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 02:45:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892205#M352407</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-01T02:45:19Z</dc:date>
    </item>
    <item>
      <title>Re: Set; inside loop in SAS in two different ways</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892247#M352421</link>
      <description>&lt;PRE&gt;You could take code

Data class1 ; 
Do I=1 to 2 ; 
Set sashelp.class; 
End;
Run;

As this

Data class1 ; 
Do I=1 to 2 ; 
Set sashelp.class; 
End;
output;   /*&amp;lt;--*/
Run;


Therefore, it would output the 2nd,4th,6th,8th,10th,12th..... obs.&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1693568286840.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87501iC7964AD817576313/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1693568286840.png" alt="Ksharp_0-1693568286840.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2023 11:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-inside-loop-in-SAS-in-two-different-ways/m-p/892247#M352421</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-09-01T11:37:40Z</dc:date>
    </item>
  </channel>
</rss>

