<?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 Conditional keep i.e. If then keep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817399#M322655</link>
    <description>&lt;P&gt;Can anyone explain why:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-This is OK:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=. then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;-But this is NOT OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=1 then keep;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;-Yet this is OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;???&lt;/P&gt;</description>
    <pubDate>Fri, 10 Jun 2022 03:38:14 GMT</pubDate>
    <dc:creator>jjsingh04</dc:creator>
    <dc:date>2022-06-10T03:38:14Z</dc:date>
    <item>
      <title>Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817399#M322655</link>
      <description>&lt;P&gt;Can anyone explain why:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-This is OK:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=. then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;-But this is NOT OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=1 then keep;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;-Yet this is OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;???&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 03:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817399#M322655</guid>
      <dc:creator>jjsingh04</dc:creator>
      <dc:date>2022-06-10T03:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817402#M322657</link>
      <description>&lt;P&gt;Different rules.&lt;/P&gt;
&lt;P&gt;You may not be aware that KEEP is a specific statement, with a companion DROP, that relates to whether specific &lt;STRONG&gt;variables&lt;/STRONG&gt; appear in a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you read the LOG it is actually fairly clear:&lt;/P&gt;
&lt;PRE&gt;384  data want;
385  set have;
386  if home=1 then keep;
                    -----
                    180 415
ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING 415-185: No KEEP variables found, statement is ignored.

387  run;

&lt;/PRE&gt;
&lt;P&gt;The underscores in the log are indicating exactly what is not in a valid location, KEEP.&lt;/P&gt;
&lt;P&gt;The Warning tells you that Keep expects a list of variables. Also Keep is not actually executable. If you read the documentation at &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/n1nnrzzsw6rzrjn1p2jfky6pdv23.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/n1nnrzzsw6rzrjn1p2jfky6pdv23.htm&lt;/A&gt; you will see that the Type of statement is Declarative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also from the documentation:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV id="p10bvg3wauedhan1qly0hiokirlv" class="xis-topic"&gt;
&lt;DIV id="p1k16go42b9kyxn18c6u0ul5qn9v" class="xis-subTopic"&gt;
&lt;H2 class="xis-title"&gt;Executable and &lt;A target="_blank" name="~1"&gt;&lt;/A&gt;&lt;FONT style="background-color: #fcdec0;"&gt;Declarative&lt;/FONT&gt; Statements&lt;/H2&gt;
&lt;DIV id="p00048m6k2zlemn1pqwrdn7t4yfg" class="xis-topicContent"&gt;
&lt;DIV id="p1241r614j1njpn1e6buft638ymh" class="xis-paragraph"&gt;DATA step statements are executable or &lt;FONT style="background-color: #fcdec0;"&gt;declarative&lt;/FONT&gt; statements that can appear in the DATA step. &lt;SPAN class="xis-windowItem"&gt;Executable statements&lt;/SPAN&gt; result in some action during individual iterations of the DATA step; &lt;SPAN class="xis-windowItem"&gt;&lt;FONT style="background-color: #fcdec0;"&gt;declarative&lt;/FONT&gt; statements&lt;/SPAN&gt; supply information to SAS and take effect when the system compiles program statements.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The companion instruction to Delete is Output to write records to the data set at a given point. However that means that once you use an Output only Output statements write to the data set(s). Normally there is an implied "output" at the end of data step code as the only time data is written.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IF &amp;lt;condition&amp;gt;; statement is a special form of the If called a "subsetting if" and means only use records meating the condition. But it does not write the set explicitly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 14:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817402#M322657</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-10T14:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817403#M322658</link>
      <description>&lt;P&gt;I think I understand. I had forgotten for a moment there that keep was the opposite of drop (not of delete), and that keep &amp;amp; drop refer to variables (not values). Thanks for bringing both points to my acute attention.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From what I now understand of declarative vs. executable, declarative statements (like keep and drop) cannot be conditional, whereas executable statements (like if-then, do-while, etc.) can definitely be conditional.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not quite sure what you mean about an output statement meaning all statements must be output only--does that mean you couldn't for example use a drop or keep statement (for a different purpose) afterwards?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 04:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817403#M322658</guid>
      <dc:creator>jjsingh04</dc:creator>
      <dc:date>2022-06-10T04:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817416#M322661</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/327936"&gt;@jjsingh04&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not quite sure what you mean about an output statement meaning all statements must be output only--does that mean you couldn't for example use a drop or keep statement (for a different purpose) afterwards?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If no explicit OUTPUT statement is written into a data step, then the compiler inserts an implicit OUTPUT on its own at the end of the data step iteration. As soon as at least one explicit OUTPUT is present, the implicit one is omitted, and all output operations must be taken care of by the programmer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since a subsetting IF does an immediate jump to the "top" of the data step (if the condition is not met), it also prevents execution of an implicit OUTPUT.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 07:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817416#M322661</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-10T07:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817562#M322708</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;Is a subsetting IF an explicit OUTPUT statement? I.e. in this case...&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if home=1;
drop leverage;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Would the resulting dataset no longer have the variable leverage--and only contain all observations where home=1? Or would there be a problem dropping the variable leverage?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 01:57:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817562#M322708</guid>
      <dc:creator>jjsingh04</dc:creator>
      <dc:date>2022-06-11T01:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817569#M322711</link>
      <description>&lt;P&gt;An explicit OUTPUT statement has to be written by the programmer. Your step has the "usual" implicit output which the compiler creates on its own because no explicit OUTPUT was given.&lt;/P&gt;
&lt;P&gt;For your other questions: create some fake "have" data and try it, then look at the result.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 05:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817569#M322711</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-11T05:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817588#M322715</link>
      <description>&lt;P&gt;You are confusing statements that are executed for each observation in the data with statements that are executed only once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most statements are executed for each observation: assignment, logical IF, subsetting IF, ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, some statements in the DATA step are executed only one time. This includes LENGTH, ARRAY, KEEP, DROP, WHERE,...&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 11:16:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817588#M322715</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-06-11T11:16:12Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817617#M322731</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Thanks for the info&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 22:39:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817617#M322731</guid>
      <dc:creator>jjsingh04</dc:creator>
      <dc:date>2022-06-11T22:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817618#M322732</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;Thanks for the info&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 22:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817618#M322732</guid>
      <dc:creator>jjsingh04</dc:creator>
      <dc:date>2022-06-11T22:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817622#M322735</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You are confusing statements that are executed for each observation in the data with statements that are executed only once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most statements are executed for each observation: assignment, logical IF, subsetting IF, ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, some statements in the DATA step are executed only one time. This includes LENGTH, ARRAY, KEEP, DROP, WHERE,...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That terminology is just going to confuse things.&lt;/P&gt;
&lt;P&gt;Declarative statements are never executed.&amp;nbsp; All of the statements are "evaluated" when the data step is first seen.&amp;nbsp; If they are just declarative then they have no role while the data step actually runs.&amp;nbsp; Only the actual executable statements have any potential for doing something while the data step runs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some statements, like SET, are really both.&amp;nbsp; For the SET statement the set of variables that the referenced dataset(s) have impact the set of variables that will exist even if there is no way the executable aspects (reading in the actual data) can ever happen.&lt;/P&gt;
&lt;P&gt;Consider this step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  stop;
   set sashelp.class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How many times will the data step iterate?&lt;/P&gt;
&lt;P&gt;How many observations will the WANT dataset have?&lt;/P&gt;
&lt;P&gt;How many variables?&lt;/P&gt;
&lt;LI-SPOILER&gt;The set will execute one iteration.&amp;nbsp; That iteration will immediate finish when it executes the STOP statement. So it will never reach the end and execute the implicit output statement (do not pass GO, do not collect $200)&lt;BR /&gt;&lt;BR /&gt;But the dataset will have all of the same variables as SASHELP.CLASS because when SAS was examining (some say compiling) the data step it saw that SASHELP.CLASS was an input dataset so it added its variables.&lt;/LI-SPOILER&gt;</description>
      <pubDate>Sun, 12 Jun 2022 02:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817622#M322735</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-12T02:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional keep i.e. If then keep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817632#M322740</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Thanks for the info. I tried running your code, and saw that it created the five variables from sashelp.class in a work.want dataset, but stopped short of populating any observations. The SET statement, combined with the STOP statement in this way, does indeed make for an illustrative example of your point here. With the SET statement, the variables are created (the declarative part)--and with the DATA statement, these variables are put into a new dataset--but with the STOP statement in the way, the observations from the source dataset cannot be recorded into the new dataset (the executable part).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jun 2022 05:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-keep-i-e-If-then-keep/m-p/817632#M322740</guid>
      <dc:creator>jjsingh04</dc:creator>
      <dc:date>2022-06-12T05:33:55Z</dc:date>
    </item>
  </channel>
</rss>

