<?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 if X then stop data statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670243#M201161</link>
    <description>&lt;P&gt;Hi everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 1000 tables 100gb each. I am processing them one by one with simple data steps.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write an if statement that would check the first row, and if column X is not equal to 1 then stop this data step (meaning stop to work with this dataset) and go to the next table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Sat, 18 Jul 2020 01:28:47 GMT</pubDate>
    <dc:creator>eduard1231</dc:creator>
    <dc:date>2020-07-18T01:28:47Z</dc:date>
    <item>
      <title>if X then stop data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670243#M201161</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 1000 tables 100gb each. I am processing them one by one with simple data steps.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write an if statement that would check the first row, and if column X is not equal to 1 then stop this data step (meaning stop to work with this dataset) and go to the next table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 01:28:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670243#M201161</guid>
      <dc:creator>eduard1231</dc:creator>
      <dc:date>2020-07-18T01:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: if X then stop data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670244#M201162</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if x ne 1 then stop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Are you positive X is an integer?&amp;nbsp; If not then you might need to add some fuzz.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if -1E5 &amp;lt;= x-1 &amp;lt;= 1E5 then stop;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Jul 2020 01:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670244#M201162</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-18T01:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: if X then stop data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670250#M201167</link>
      <description>&lt;P&gt;Question: Are you going to produce a single dataset from all the qualifying datasets, or must you create one dataset for each qualifying dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the answer is "create 1 dataset from all qualifying datasets", then you could process all 1,000 in a single step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have: curobs=cobs  indsname=dsnam;
  retain _skipdata ' ';
  if cobs=1 then _skipdata=ifc(x^=1,'Y','N');
  if _skipdata='N';
  sourcedsn=dsnam; 
  *** Your other code here ***;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;SET HAVE:&amp;nbsp; &amp;nbsp;, with the trailing : means read all datasets whose names begin with 'HAVE'.&amp;nbsp; You could also say things like SET HAVE:&amp;nbsp; &amp;nbsp;OLD:&amp;nbsp; .&lt;/LI&gt;
&lt;LI&gt;The curobs= tells SAS to put the current observation number in variable COBS.&amp;nbsp; And cobs is the observation number relative to each incoming dataset, not to the aggregate of all the datasets.&lt;/LI&gt;
&lt;LI&gt;So the "if cobs=1 then ...." checks the value of X for the first obs of each dataset.&lt;/LI&gt;
&lt;LI&gt;I assume that you contemplate running the same code for each of the 1,000 datasets you refer to, so you can use a single copy of that code in this data step.&lt;/LI&gt;
&lt;LI&gt;I put in the "sourcedsn=dsnam" statement just in case you want to track the numerous source datasets for each record in the single output dataset.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;But if you want one output data set for each qualifying input dataset&amp;nbsp; (i.e. you want work.new_have1 from work.have1,&amp;nbsp; work.new_have4 from work.have4), then you could:&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;%macro t (indsn=,outdsn=)/ des='Code to apply to each dataset';
  data &amp;amp;outdsn;
    set &amp;amp;indsn;
    *** other code here ***;
  run;
%mend t;

data _null_;
  set have: (obs=1 keep=x) curobs=cobs indsname=dsname ;
  if x=1;
  outdsn='new_' || scan(dsname,2,'.');
  call execute(cats('*%t(indsn=',dsname,',outdsn=',outdsn,');'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The objective of this strategy is to avoid creating even a dummy output dataset for those original datasets that do not qualify.&lt;/LI&gt;
&lt;LI&gt;The use of the macro T is based on the assumption that you want to do the same process to each qualifying dataset (in this case each qualifying "HAVE..." dataset.&lt;/LI&gt;
&lt;LI&gt;The "obs=1" parameter just tells SAS to read only the first obs from each HAVE... dataset.&lt;/LI&gt;
&lt;LI&gt;The CALL EXECUTE statement sets up a call to macro T just for those datasets that meet your criterion in their first observation.&lt;/LI&gt;
&lt;LI&gt;Because the INDSNAME= parametr returns a two-level dataset name, you see a SCAN function that tracks only the second level&amp;nbsp; (i.e.&amp;nbsp; HAVE1 from WORK.HAVE1) in naming a new dataset.&lt;/LI&gt;
&lt;LI&gt;This program reads every record from every dataset, but it only keeps records from qualifying datasets.&amp;nbsp; So if the non-qualifying datasets are large, and also comprise a large proportion of candidate datasets, then this strategy could become inefficient.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 03:23:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-X-then-stop-data-statement/m-p/670250#M201167</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-18T03:23:34Z</dc:date>
    </item>
  </channel>
</rss>

