<?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: DOW LOOP HELP in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107576#M29919</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Indeed I said that in the previous post. Two ifs with mutually exclusive conditions cannot both be true at the same time so you will not have duplicate output from not using an ELSE statement. You simply slightly reduce program efficiency&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 09 Oct 2013 15:37:33 GMT</pubDate>
    <dc:creator>Vince28_Statcan</dc:creator>
    <dc:date>2013-10-09T15:37:33Z</dc:date>
    <item>
      <title>DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107568#M29911</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi...&lt;/P&gt;&lt;P&gt;Is the folllwing two codes doing the SAME for the below data?&lt;/P&gt;&lt;P&gt;if true can someone explain the do until code???????why two SET stmnts used etc etc???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1)IF we have&lt;SPAN style="color: #ff00ff;"&gt; two or more consecutive INs&lt;/SPAN&gt; then we need to &lt;SPAN style="color: #ff0000;"&gt;take the first IN&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2)if we have &lt;SPAN style="background-color: #ff00ff;"&gt;two or more consecutive OUTS&lt;/SPAN&gt; then we need to &lt;SPAN style="color: #ff0000;"&gt;take the LAST OUT&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;do until(last.flag);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do until(last.flag);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='OUT' and last.flag then output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='OUT' and last.flag then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff0000;"&gt;HAVE&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; recorded_time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp; 01AUG2013:10:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt; 101&amp;nbsp;&amp;nbsp; 01AUG2013:14:15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;101&amp;nbsp;&amp;nbsp; 01AUG2013:17:15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;102&amp;nbsp;&amp;nbsp; 02AUG2013:19:23&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;102&amp;nbsp;&amp;nbsp; 02AUG2013:21:04&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;102&amp;nbsp;&amp;nbsp; 11AUG2013:01:01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt; 102&amp;nbsp;&amp;nbsp; 11AUG2013:01:30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;103&amp;nbsp; 09AUG2013:19:25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;103&amp;nbsp; 14AUG2013:14:35&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;103&amp;nbsp; 14AUG2013:23:01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;103&amp;nbsp; 14AUG2013:23:12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;103&amp;nbsp; 17AUG2013:11:20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;104&amp;nbsp; 02AUG2013:04:02&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt; 104&amp;nbsp; 08AUG2013:15:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&amp;nbsp;&amp;nbsp; &lt;BR /&gt;104&amp;nbsp; 20AUG2013:14:11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;104&amp;nbsp; 20AUG2013:17:12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;105&amp;nbsp; 07AUG2013:02:50&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;105&amp;nbsp; 07AUG2013:02:55&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;105&amp;nbsp; 11AUG2013:14:00&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;105&amp;nbsp; 16AUG2013:14:10&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;106&amp;nbsp; 02AUG2013:08:15&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;107&amp;nbsp; 20AUG2013:13:05&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;108&amp;nbsp; 12AUG2013:11:00&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;108&amp;nbsp; 12AUG2013:18:37&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;108&amp;nbsp; 17AUG2013:03:55&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;108&amp;nbsp; 22AUG2013:11:25&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;109&amp;nbsp; 06AUG2013:18:55&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt; 109&amp;nbsp; 07AUG2013:11:20&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;109&amp;nbsp; 13AUG2013:00:09&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;109&amp;nbsp; 14AUG2013:10:35&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff0000;"&gt;WANT&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; recorded_time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag&lt;/P&gt;&lt;P&gt;&lt;BR clear="all" /&gt;101&amp;nbsp;&amp;nbsp; 01AUG2013:10:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt;101&amp;nbsp;&amp;nbsp; 01AUG2013:17:15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;102&amp;nbsp;&amp;nbsp; 02AUG2013:21:04&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;102&amp;nbsp;&amp;nbsp; 11AUG2013:01:01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt; 102&amp;nbsp;&amp;nbsp; 11AUG2013:01:30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;103&amp;nbsp; 09AUG2013:19:25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;103&amp;nbsp; 14AUG2013:14:35&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;103&amp;nbsp; 14AUG2013:23:01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;103&amp;nbsp; 17AUG2013:11:20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;104&amp;nbsp; 02AUG2013:04:02&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt; 104&amp;nbsp; 08AUG2013:15:00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&amp;nbsp; &lt;/P&gt;&lt;P&gt;104&amp;nbsp; 20AUG2013:17:12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;105&amp;nbsp; 07AUG2013:02:50&amp;nbsp;&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt;105&amp;nbsp; 16AUG2013:14:10&amp;nbsp;&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;106&amp;nbsp; 02AUG2013:08:15&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;107&amp;nbsp; 20AUG2013:13:05&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt;108&amp;nbsp; 12AUG2013:11:00&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt;108&amp;nbsp; 22AUG2013:11:25&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;109&amp;nbsp; 06AUG2013:18:55&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt; 109&amp;nbsp; 07AUG2013:11:20&amp;nbsp;&amp;nbsp; OUT&lt;BR /&gt;109&amp;nbsp; 13AUG2013:00:09&amp;nbsp;&amp;nbsp; IN&lt;BR /&gt;109&amp;nbsp; 14AUG2013:10:35&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 09:00:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107568#M29911</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2013-10-09T09:00:52Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107569#M29912</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Robert,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;At first glance, both codes do produce the same results. The only distinction is that in the first approach doesn't increment _N_ the same way as it uses do blocks. However, the first do until block is redundant with the second. You are essentially reading however many rows there are in your by group, doing nothing with it and then reading them all again to do the desired conditionnal processing in the second do block.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It should be simplified as&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do until(last.flag);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='OUT' and last.flag then output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an experiment, try the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; do until(last.flag);&lt;BR /&gt;&amp;nbsp; i=_N_;&lt;BR /&gt;&amp;nbsp; set have;&lt;BR /&gt;&amp;nbsp; by id flag notsorted;&lt;BR /&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;BR /&gt;&amp;nbsp; if flag='OUT' and last.flag then output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want2;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;nbsp; i=_N_;&lt;BR /&gt;&amp;nbsp; by id flag notsorted;&lt;BR /&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;BR /&gt;&amp;nbsp; if flag='OUT' and last.flag then output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will depict the slight subtlety between the 2. Please note also that because the first code is enclosed within DO block, it means that the PDV only gets reset to missing each time _N_ iterates. Thus, if you calculated any new column from your data conditionnally, the values would be kept as though you had a retain statement by group. However, this is a non-issue with your example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vince&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 12:13:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107569#M29912</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-09T12:13:59Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107570#M29913</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for the response. If you take 109 for example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;109&amp;nbsp; 06AUG2013:18:55&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt; 109&amp;nbsp; 07AUG2013:11:20&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;109&amp;nbsp; 13AUG2013:00:09&amp;nbsp;&amp;nbsp; IN&lt;/P&gt;&lt;P&gt;109&amp;nbsp; 14AUG2013:10:35&amp;nbsp;&amp;nbsp; OUT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want2;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; i=_N_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='OUT' and last.flag then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;because of the non sorted options within the same ID we have many first.'s and last.'s instead of having&lt;/P&gt;&lt;P&gt;one first. and one last.??????&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 13:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107570#M29913</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2013-10-09T13:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107571#M29914</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The not sorted allows you not to receive an error message because your data is not sorted according to your BY variables. I've assumed from your example that it was intended to get each pair of in/out and that all you wanted to remove from your data were the successive INs or OUTs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The not sorted options allows you to retain the chronological ordering of your data and creates new by groups each time there is a change in a by group variable even if the same group existed before. So yes, it allows you to have many firsts and lasts for each by group for so long as they are separated by a different by group.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you wanted to get strictly the earliest IN and lastest OUT for each ID rather than each pair of alternate, you would've done&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id flag recorded_time;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id flag;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.flag and flag='IN' then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last.flag and flag='OUT' then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The re-sorting by flag before the chronological series allows you to retain strictly one 'IN' and one 'OUT' value. Since your data is sorted according to your by variables, the notsorted option would have no effect.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Not sure that answers your question - hope it helps.&lt;/P&gt;&lt;P&gt;Vince&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 14:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107571#M29914</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-09T14:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107572#M29915</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Vince,&lt;/P&gt;&lt;P&gt;I get what you are trying to explain...&lt;/P&gt;&lt;P&gt;My only concern and thought is&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;wont there be duplicates because of using the SET statement twice???&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;do until(last.flag);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;do until(last.flag);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id flag notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if flag='IN' and first.flag then output;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="color: #ff6600;"&gt; if flag='OUT' and last.flag then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*ALSO CAN I USE THE &lt;SPAN style="color: #ff0000;"&gt;ELSE IF&lt;/SPAN&gt; &lt;SPAN style="color: #ff6600;"&gt;instead of IF&lt;/SPAN&gt; condition in the second IF condition???&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 14:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107572#M29915</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2013-10-09T14:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107573#M29916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No. Your output is controlled by the OUTPUT statements in the two conditionnal statements in the second DO loop. Thus, as I mentioned in my first post, the first DO block is useless code. It only slows your program down.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since both set statement use a different copy of the same dataset, there is a single PDV with the variables in HAVE. The PDV gets overwritten for each set statement in the first do block nothing gets output. The second do block begins, reads from "another copy" of HAVE and overwrites the PDV until one of the two IF conditions are met in which case there is an output (another copy conceptually that is not exactly how it works in practice - I believe that each set statement uses a different pointer, think like an internal _N_ to the set statement so they both read through the entire dataset and the rows read in the first block have no impact on the rows read in the second block).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not sure my explanation is great above but anyway it gives some insight on how the set statements apply to the PDV and why the first DO block is useless to the desired output. DO UNTIL blocks are always entered because the until condition only applies at the end of the do block. Thus, even if you are at last.flag from the first block, you still enter the second block and then last.flag is set to 0 and first.flag is set to 1 from the new row read from set have.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 15:09:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107573#M29916</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-09T15:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107574#M29917</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, you could use an ELSE IF because the conditions are mutually exclusive. In fact, it would save you from testing the second condition for each first.flag thus slightly improving the program efficiency.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Be aware though, first.flag and last.flag are not mutually exclusive, the empty intersection is that of flag='IN' and flag='OUT'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;if first.flag then output;&lt;/P&gt;&lt;P&gt;if last.flag then output;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;behaves differently from&lt;/P&gt;&lt;P&gt;if first.flag then output;&lt;/P&gt;&lt;P&gt;else if last.flag then output;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the former, anytime there is a single element in a by flag group will be output twice as it is both the first and the last flag of the group so you will absolutely always have 2 rows produced for each by flag group.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the later, the else removes double printing whenever a group has a single element.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 15:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107574#M29917</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-09T15:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107575#M29918</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for the detailed explanation....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my case even if there is a single element "I am saying&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; if flag='IN' and first.flag then output;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="color: #ff6600;"&gt; if flag='OUT' and last.flag then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff6600;"&gt;I am specifying what the flag is so i think it would not pull any duplicates?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff6600;"&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 15:21:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107575#M29918</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2013-10-09T15:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: DOW LOOP HELP</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107576#M29919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Indeed I said that in the previous post. Two ifs with mutually exclusive conditions cannot both be true at the same time so you will not have duplicate output from not using an ELSE statement. You simply slightly reduce program efficiency&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 15:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DOW-LOOP-HELP/m-p/107576#M29919</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-10-09T15:37:33Z</dc:date>
    </item>
  </channel>
</rss>

