<?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 Iterative DO statement with WHILE expression in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508384#M1653</link>
    <description>&lt;P&gt;I am reviewing for the Base SAS certification and was questioning one of the answers in the book. &amp;nbsp;The question is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In the following program, complete the statement so that the program stops generating observations when Distance reaches 250 miles or when 10 gallons of fuel have been used.&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;data work.go250;&amp;nbsp;
&amp;nbsp; set cert.cars;&amp;nbsp;
&amp;nbsp; do gallons=1 to 10 ... ;&amp;nbsp;
&amp;nbsp; &amp;nbsp; Distance=gallons*mpg;&amp;nbsp;
&amp;nbsp; &amp;nbsp; output;&amp;nbsp;
&amp;nbsp; end;&amp;nbsp;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The correct answer, according to the book, is&amp;nbsp;while(Distance&amp;lt;=250). However, I believe the answer is&amp;nbsp;until(Distance=250).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My understanding of the WHILE expression is that it&amp;nbsp;&lt;SPAN&gt;i&lt;/SPAN&gt;&lt;SPAN&gt;s evaluate&lt;/SPAN&gt;&lt;SPAN&gt;d before&amp;nbsp;the execution of the DO loop, so if Distance reaches 250, the do loop will still execute&amp;nbsp;&lt;/SPAN&gt;because the statement (Distance &amp;lt;= 250) is still true. &amp;nbsp;I made up a dataset in SAS Studio to see what would happen with both the WHILE and UNTIL statements.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHILE statement&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;data cars;
	input  mpg;
	datalines;
50 
	;
run;

data work.go250; 
  set cars; 
  do gallons=1 to 10 while(Distance le 250); 
    Distance=gallons*mpg; 
    output; 
  end; 
run;
proc print data = go250; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	mpg	gallons	Distance
1	50	1	50
2	50	2	100
3	50	3	150
4	50	4	200
5	50	5	250
6	50	6	300&lt;/CODE&gt;&lt;/PRE&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;&lt;P&gt;UNTIL statement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.go250_2; 
  set cars; 
  do gallons=1 to 10 until(Distance=250); 
    Distance=gallons*mpg; 
    output; 
  end; 
run;

proc print data = go250_2; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	mpg	gallons	Distance
1	50	1	50
2	50	2	100
3	50	3	150
4	50	4	200
5	50	5	250&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Based on the output, it looks like the UNTIL statement satisfies the requirement of "...&lt;SPAN&gt;Distance reaches 250 miles..."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any insights would be appreciated. Thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Oct 2018 15:29:37 GMT</pubDate>
    <dc:creator>he2185</dc:creator>
    <dc:date>2018-10-29T15:29:37Z</dc:date>
    <item>
      <title>Iterative DO statement with WHILE expression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508384#M1653</link>
      <description>&lt;P&gt;I am reviewing for the Base SAS certification and was questioning one of the answers in the book. &amp;nbsp;The question is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In the following program, complete the statement so that the program stops generating observations when Distance reaches 250 miles or when 10 gallons of fuel have been used.&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;data work.go250;&amp;nbsp;
&amp;nbsp; set cert.cars;&amp;nbsp;
&amp;nbsp; do gallons=1 to 10 ... ;&amp;nbsp;
&amp;nbsp; &amp;nbsp; Distance=gallons*mpg;&amp;nbsp;
&amp;nbsp; &amp;nbsp; output;&amp;nbsp;
&amp;nbsp; end;&amp;nbsp;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The correct answer, according to the book, is&amp;nbsp;while(Distance&amp;lt;=250). However, I believe the answer is&amp;nbsp;until(Distance=250).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My understanding of the WHILE expression is that it&amp;nbsp;&lt;SPAN&gt;i&lt;/SPAN&gt;&lt;SPAN&gt;s evaluate&lt;/SPAN&gt;&lt;SPAN&gt;d before&amp;nbsp;the execution of the DO loop, so if Distance reaches 250, the do loop will still execute&amp;nbsp;&lt;/SPAN&gt;because the statement (Distance &amp;lt;= 250) is still true. &amp;nbsp;I made up a dataset in SAS Studio to see what would happen with both the WHILE and UNTIL statements.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHILE statement&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;data cars;
	input  mpg;
	datalines;
50 
	;
run;

data work.go250; 
  set cars; 
  do gallons=1 to 10 while(Distance le 250); 
    Distance=gallons*mpg; 
    output; 
  end; 
run;
proc print data = go250; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	mpg	gallons	Distance
1	50	1	50
2	50	2	100
3	50	3	150
4	50	4	200
5	50	5	250
6	50	6	300&lt;/CODE&gt;&lt;/PRE&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;&lt;P&gt;UNTIL statement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.go250_2; 
  set cars; 
  do gallons=1 to 10 until(Distance=250); 
    Distance=gallons*mpg; 
    output; 
  end; 
run;

proc print data = go250_2; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	mpg	gallons	Distance
1	50	1	50
2	50	2	100
3	50	3	150
4	50	4	200
5	50	5	250&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Based on the output, it looks like the UNTIL statement satisfies the requirement of "...&lt;SPAN&gt;Distance reaches 250 miles..."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any insights would be appreciated. Thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 15:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508384#M1653</guid>
      <dc:creator>he2185</dc:creator>
      <dc:date>2018-10-29T15:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative DO statement with WHILE expression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508388#M1655</link>
      <description>&lt;P&gt;First, the WHILE condition would be correct, but needs to remove the equal sign:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;while (distance &amp;lt; 250)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, UNTIL would be incorrect because UNTIL always executes the first time.&amp;nbsp; It doesn't check the truth or falsity of the condition at the top of the loop, but only checks it at the END statement.&amp;nbsp; So if your original DISTANCE was 300, UNTIL would still execute (improperly) once.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 15:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508388#M1655</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-29T15:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative DO statement with WHILE expression</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508411#M1656</link>
      <description>&lt;P&gt;Did the possibly answers include any option with the LEAVE statement?&lt;/P&gt;
&lt;P&gt;That would be another option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   mpg = 28.2;
   do gallons=1 to 10;
      distance= gallons * mpg;
      if distance &amp;gt; 250 then leave;
      output;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;whether the LEAVE comes before or after the output would depend on the actual intent of the rule.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 17:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-DO-statement-with-WHILE-expression/m-p/508411#M1656</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-29T17:10:40Z</dc:date>
    </item>
  </channel>
</rss>

