<?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: Conditional output if one dataset is empty? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976183#M378268</link>
    <description>&lt;P&gt;Thanks for taking the time to respond,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are correct that I am trying to only write observations for BY groups that appear in both datasets. &amp;nbsp;But if dataset one is empty, I would like the code to simply pass dataset two through to dataset want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;</description>
    <pubDate>Wed, 01 Oct 2025 21:01:41 GMT</pubDate>
    <dc:creator>genemroz</dc:creator>
    <dc:date>2025-10-01T21:01:41Z</dc:date>
    <item>
      <title>Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976145#M378262</link>
      <description>&lt;P&gt;I'm using this datastep to interleave two datasets. &amp;nbsp;I need to include an option to output dataset opt4interleave to dataset interleave in the event that the dataset fixed4interleave is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for any advice you can offer,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data interleave;
format target $20.;
&amp;nbsp; do _N_=1 by 1 until(last.Target);
&amp;nbsp; &amp;nbsp; set fixed4interleave (in=ina) opt4interleave (in=inb);
&amp;nbsp; &amp;nbsp; by Target;
&amp;nbsp; &amp;nbsp; N_A+ina;
&amp;nbsp; &amp;nbsp; N_B+inb;
&amp;nbsp; end;
&amp;nbsp; do _N_=1 to _N_;
&amp;nbsp; &amp;nbsp; set fixed4interleave opt4interleave curobs=curobs1;
&amp;nbsp; &amp;nbsp; by Target; &amp;nbsp; &amp;nbsp;
if N_A and N_B then output;
&amp;nbsp; end;&amp;nbsp;
&amp;nbsp; call missing(N_A,N_B);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 01 Oct 2025 17:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976145#M378262</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2025-10-01T17:29:45Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976148#M378265</link>
      <description>&lt;P&gt;Check NOBS in DICTIONARY.TABLES for your dataset, and then use %IF-%THEN-%DO to run code conditionally:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nobs into :mynobs
from dictionary.tables
where libname = "WORK" and memname = "FIXED4INTERLEAVE";
quit;

%if &amp;amp;mynobs.
%then %do;
/* your code */
%end;
%else %do;
data interleave;
set opt4interleave;
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Oct 2025 21:09:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976148#M378265</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-10-01T21:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976149#M378266</link>
      <description>&lt;P&gt;Your description is too short to understand exactly what you mean.&amp;nbsp; And I don't understand what your posted data step is trying to do.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To interleave two datasets just use code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set one two;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is it that you want to do differently than that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to exclude BY groups from TWO that do not appear in ONE then add a retained variable to remember if there were any observations for that group in ONE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set one(in=in1) two(in=in2);
  by id;
  if first.id then any=0;
  retain any;
  if in1 then any=1;
  if in2 and not any then delete;
  drop any ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Oct 2025 18:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976149#M378266</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-10-01T18:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976180#M378267</link>
      <description>&lt;P&gt;Perhaps you are trying to only write observations for BY groups that appear in both?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge one(keep=id in=in1) two(keep=id in=in2);
  by id;
  if first.id;
  do until (last.id);
     set one two;
     by id;
     if in1 and in2 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Oct 2025 20:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976180#M378267</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-10-01T20:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976183#M378268</link>
      <description>&lt;P&gt;Thanks for taking the time to respond,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are correct that I am trying to only write observations for BY groups that appear in both datasets. &amp;nbsp;But if dataset one is empty, I would like the code to simply pass dataset two through to dataset want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;</description>
      <pubDate>Wed, 01 Oct 2025 21:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976183#M378268</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2025-10-01T21:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976187#M378269</link>
      <description>&lt;P&gt;So check that also.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if 0 then set one nobs=nobs;
  merge one(keep=id in=in1) two(keep=id in=in2);
  by id;
  if first.id;
  do until (last.id);
     set one two;
     by id;
     if (in1 or 0=nobs) and in2 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the first dataset is a view that prevents NOBS from working then just add an extra step to set a macro variable instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputx('nobs',0);
  set one;
  call symputx('nobs',1);
  stop;
run;
data want;
  merge one(keep=id in=in1) two(keep=id in=in2);
  by id;
  if first.id;
  do until (last.id);
     set one two;
     by id;
     if (in1 or 0=&amp;amp;nobs) and in2 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Oct 2025 22:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976187#M378269</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-10-01T22:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976191#M378270</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;I adopted your suggested code to my problem but dataset want was not populated with dataset two. &amp;nbsp;See below for log output. &amp;nbsp;Thanks for trying to help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gene&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="sasSource"&gt;data want;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;if 0 then set fixed4interleave nobs=nob;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;merge fixed4interleave(in=in1) opt4interleave (in=in2);&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;by target;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;if first.target;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;do until (last.target);&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;set fixed4interleave opt4interleave;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;by target;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;if (in1 or 0=nobs) and in2 then output;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;end;&lt;/DIV&gt;
&lt;DIV class="sasSource"&gt;run;&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="sasLogNote71_1759359511603" class="sasNote"&gt;NOTE: Variable nobs is uninitialized.&lt;/DIV&gt;
&lt;DIV id="sasLogNote72_1759359511603" class="sasNote"&gt;NOTE: There were 0 observations read from the data set WORK.FIXED4INTERLEAVE.&lt;/DIV&gt;
&lt;DIV id="sasLogNote73_1759359511603" class="sasNote"&gt;NOTE: There were 399680 observations read from the data set WORK.OPT4INTERLEAVE.&lt;/DIV&gt;
&lt;DIV id="sasLogNote74_1759359511603" class="sasNote"&gt;NOTE: There were 0 observations read from the data set WORK.FIXED4INTERLEAVE.&lt;/DIV&gt;
&lt;DIV id="sasLogNote75_1759359511603" class="sasNote"&gt;NOTE: There were 399680 observations read from the data set WORK.OPT4INTERLEAVE.&lt;/DIV&gt;
&lt;DIV id="sasLogNote76_1759359511603" class="sasNote"&gt;NOTE: The data set WORK.WANT has 0 observations and 6 variables.&lt;/DIV&gt;</description>
      <pubDate>Wed, 01 Oct 2025 23:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976191#M378270</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2025-10-01T23:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976192#M378271</link>
      <description>&lt;P&gt;You have a typo (or perhaps I did?).&lt;/P&gt;
&lt;P&gt;Notice that SAS tells you about it:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;NOTE: Variable nobs is uninitialized.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fix this line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if 0 then set fixed4interleave nobs=&lt;FONT color="#FF0000"&gt;nob&lt;/FONT&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Oct 2025 23:23:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976192#M378271</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-10-01T23:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional output if one dataset is empty?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976194#M378272</link>
      <description>Thanks, Tom, for helping me through this.&lt;BR /&gt;&lt;BR /&gt;Gene</description>
      <pubDate>Wed, 01 Oct 2025 23:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-output-if-one-dataset-is-empty/m-p/976194#M378272</guid>
      <dc:creator>genemroz</dc:creator>
      <dc:date>2025-10-01T23:34:54Z</dc:date>
    </item>
  </channel>
</rss>

