<?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: If else error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587827#M167913</link>
    <description>&lt;P&gt;You have a statement between the IF and the ELSE.&lt;/P&gt;
&lt;P&gt;Note that you do NOT need the ELSE.&amp;nbsp; Your IF condition is always going to be false so whatever statement you wanted to put after the ELSE can just be run unconditionally since you want it to always run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
  if 0 then set dis_entity_id;
  call missing(of _all_);
  set dis_entity_id;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It looks like you are just trying create a dataset with one observation that has all missing values. (why???)&lt;/P&gt;
&lt;P&gt;That can be done then use one of these. The shorter one will get a NOTE in the log about stopping because of looping.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
  if 0 then set dis_entity_id;
run;

data vars;
  if 0 then set dis_entity_id;
  output;
  stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to create a dataset with zero observations use one of these:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
  set dis_entity_id;
  stop;
run;
data vars;
  set dis_entity_id(obs=0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Sep 2019 13:19:01 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-09-11T13:19:01Z</dc:date>
    <item>
      <title>If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587815#M167909</link>
      <description>&lt;P&gt;I tried the code as follows and ended up with error. If there is any observation in the dataset then I want to get that value in 'vars' dataset and if no Observation I want to get missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;2859       data vars;
2860       if 0 then set dis_entity_id;
2861       call missing(of _all_);
2862       else set dis_entity_id;
           ____
           160
ERROR 160-185: No matching IF-THEN clause.

2863       output;
2864       stop;
2865       run;

NOTE: The SAS System stopped processing this step because of errors.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Sep 2019 12:49:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587815#M167909</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-11T12:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587817#M167910</link>
      <description>&lt;P&gt;ELSE must be the next command after the IF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or ... If you use the structure IF ... THEN ... DO ... END, then the ELSE must be the next command after the END&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 12:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587817#M167910</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-11T12:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587827#M167913</link>
      <description>&lt;P&gt;You have a statement between the IF and the ELSE.&lt;/P&gt;
&lt;P&gt;Note that you do NOT need the ELSE.&amp;nbsp; Your IF condition is always going to be false so whatever statement you wanted to put after the ELSE can just be run unconditionally since you want it to always run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
  if 0 then set dis_entity_id;
  call missing(of _all_);
  set dis_entity_id;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It looks like you are just trying create a dataset with one observation that has all missing values. (why???)&lt;/P&gt;
&lt;P&gt;That can be done then use one of these. The shorter one will get a NOTE in the log about stopping because of looping.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
  if 0 then set dis_entity_id;
run;

data vars;
  if 0 then set dis_entity_id;
  output;
  stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to create a dataset with zero observations use one of these:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vars;
  set dis_entity_id;
  stop;
run;
data vars;
  set dis_entity_id(obs=0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 13:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587827#M167913</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-11T13:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587862#M167926</link>
      <description>&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; vars&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; dis_entity_id&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; else set dis_entiti_id;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call &lt;SPAN class="token function"&gt;missing&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;of &lt;SPAN class="token keyword"&gt;_all_&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stop&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token punctuation"&gt;this should work fine . if and else statement shall come together .&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 13:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587862#M167926</guid>
      <dc:creator>Meghna14</dc:creator>
      <dc:date>2019-09-11T13:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587895#M167938</link>
      <description>May I know for which condition the 'call missing' will execute? I want it&lt;BR /&gt;to execute only if obs is 0 in the source dataset.&lt;BR /&gt;</description>
      <pubDate>Wed, 11 Sep 2019 15:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587895#M167938</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-11T15:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587897#M167940</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;May I know for which condition the 'call missing' will execute? I want it&lt;BR /&gt;to execute only if obs is 0 in the source dataset.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Doesn't really make any sense. If there are no values then the values are already missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What output do you want when there are 0 observations in the source?&amp;nbsp; Do you want 0 observations, 1 observation, something else?&lt;/P&gt;
&lt;P&gt;What output do you want when there is only 1 observation in the source?&lt;/P&gt;
&lt;P&gt;What output do you want when there are more than 1 observation in the source?&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 15:08:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587897#M167940</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-11T15:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587900#M167941</link>
      <description>I want to display missing value for a variable only if source dataset has 0&lt;BR /&gt;observation.&lt;BR /&gt;&lt;BR /&gt;If the source dataset has 1 or more than 1 observations then I want those&lt;BR /&gt;observation(s) in the output.&lt;BR /&gt;</description>
      <pubDate>Wed, 11 Sep 2019 15:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587900#M167941</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-11T15:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: If else error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587910#M167943</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I want to display missing value for a variable only if source dataset has 0&lt;BR /&gt;observation.&lt;BR /&gt;&lt;BR /&gt;If the source dataset has 1 or more than 1 observations then I want those&lt;BR /&gt;observation(s) in the output.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are still not describing what you want completely. Examples really help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only reason to do ANYTHING is if you want to output 1 observation when there are NO observations in the source.&lt;/P&gt;
&lt;P&gt;You could use the NOBS= option on SET statement to check how many observations, but that will not work for views and other types of datasets where the metadata of the number of observations is not available. It is probably better to use the END= option as that will work for all data sources.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to conditionally add an OUTPUT statement to get the extra observation created.&amp;nbsp; But once you have an OUTPUT statement you need to add one at the end of the step to replace the implicit OUTPUT that is normally added to a data step.&amp;nbsp; Also you need test BEFORE the SET statement because once the SET statement reads past the end of the input data the step stops. So any statements after the SET will never execute if the input dataset is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  if _n_=1 and eof then output;
  set have end=eof;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note you can simplify (obfuscate?) by changing the IF condition and eliminate the need for the extra OUTPUT statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if (_n_&amp;gt;1) or eof then output;
  set have end=eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Sep 2019 18:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-else-error/m-p/587910#M167943</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-11T18:10:29Z</dc:date>
    </item>
  </channel>
</rss>

