<?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 why doesn't DATA output statement duplicate all observations? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480299#M124123</link>
    <description>&lt;P&gt;Hi, i'm running a program and with data, as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data two (xy.txt):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;5 2
3 1
5 6&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
	infile '/folders/myfolders/sasuser.v94/xy.txt';
	input x y;
run;

data one two other;
  set two;
  if x = 5 then output one;
  if y &amp;lt; 5 then output two;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;since output statement is placed at the end of the program, without any conditions (if),&lt;/P&gt;&lt;P&gt;shouldn't the dataset two have 2 exact same observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but what I see, from the output windows of SAS is:&lt;/P&gt;&lt;P&gt;- a table with 5 observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;below, I copied and pasted from the output window.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Work.two Total rows: 5Total columns: 2&lt;/P&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 22 Jul 2018 23:17:55 GMT</pubDate>
    <dc:creator>jimmychoi</dc:creator>
    <dc:date>2018-07-22T23:17:55Z</dc:date>
    <item>
      <title>why doesn't DATA output statement duplicate all observations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480299#M124123</link>
      <description>&lt;P&gt;Hi, i'm running a program and with data, as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data two (xy.txt):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;5 2
3 1
5 6&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
	infile '/folders/myfolders/sasuser.v94/xy.txt';
	input x y;
run;

data one two other;
  set two;
  if x = 5 then output one;
  if y &amp;lt; 5 then output two;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;since output statement is placed at the end of the program, without any conditions (if),&lt;/P&gt;&lt;P&gt;shouldn't the dataset two have 2 exact same observations?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but what I see, from the output windows of SAS is:&lt;/P&gt;&lt;P&gt;- a table with 5 observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;below, I copied and pasted from the output window.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Work.two Total rows: 5Total columns: 2&lt;/P&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Jul 2018 23:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480299#M124123</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2018-07-22T23:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: why doesn't DATA output statement duplicate all observations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480305#M124125</link>
      <description>&lt;P&gt;Yes, the data set two should have two identical observations. And it does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect you mistake the observation number for an actual variable? See the code below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
input x y;
datalines;
5 2
3 1
5 6
;

data one two other;
  set two;
  if x = 5 then output one;
  if y &amp;lt; 5 then output two;
  output;
run;

proc print data=two;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Jul 2018 23:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480305#M124125</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-07-22T23:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: why doesn't DATA output statement duplicate all observations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480316#M124127</link>
      <description>&lt;P&gt;Hi:&lt;BR /&gt; The issue you're going to run into is that the last OUTPUT statement will also write observations to one and two. So you'll end up with MORE observations in ONE and TWO than you might intend. It depends on what your intention is. Consider this output and debugging version of the program shown below. In the second output and the code, the HOW_OUT variable shows you exactly HOW each observation was written to each output file.&lt;BR /&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run a version of your program (to eliminate the confusion of having data two and set two, I started with a data set called 'fakedata'), this is what I get:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="use_IF_out.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21912i1306246BF86EF8DE/image-size/large?v=v2&amp;amp;px=999" role="button" title="use_IF_out.png" alt="use_IF_out.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how each obs got into the output files -- notice the new variable called "HOW_OUT" which shows exactly which statement wrote the obs to the file:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="how_out.png" style="width: 216px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21913i0865CDA4B726085F/image-size/large?v=v2&amp;amp;px=999" role="button" title="how_out.png" alt="how_out.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;using this code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fakedata;
input x y;
datalines;
5 2
3 1
5 6
;
run;

data one two other;
  length x 8 y 8 how_out $14;
  set fakedata;
  if x = 5 then do; how_out='if x = 5'; output one; end;
  if y &amp;lt; 5 then do; how_out='if y &amp;lt; 5'; output two; end;
  how_out='final output';
  output;
run;

proc print data=fakedata noobs;
  title '0) starting with work.fakedata';
  run;
 
proc print data=one noobs;
  title '1) what is in work.one';
  run;
 
proc print data=two noobs;
  title '2) what is in work.two';
  run;

proc print data=other noobs;
  title '3) what is in work.other';
  run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Jul 2018 01:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480316#M124127</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-07-23T01:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: why doesn't DATA output statement duplicate all observations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480661#M124237</link>
      <description>Your idea to name data two to fakedata really helped me to understand, thanks</description>
      <pubDate>Mon, 23 Jul 2018 22:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480661#M124237</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2018-07-23T22:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: why doesn't DATA output statement duplicate all observations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480778#M124270</link>
      <description>&lt;P&gt;Hi, &lt;BR /&gt; That really was something I consider a best practice. In my world, it is not good to do this:&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;data &lt;FONT color="#FF0000"&gt;mydata&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; set &lt;FONT color="#FF0000"&gt;mydata&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;... more code ...;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;Because that makes it impossible to separate the INPUT data (on the SET statement) from the OUTPUT data (on the DATA statement) and could result in the loss of the INPUT data if you have any fatal errors in your code.&lt;BR /&gt;&lt;BR /&gt; I ALWAYS recommend to my students that they avoid the temptation to use the same name on both their DATA and SET statements.&lt;BR /&gt;&lt;BR /&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jul 2018 12:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-doesn-t-DATA-output-statement-duplicate-all-observations/m-p/480778#M124270</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-07-24T12:40:16Z</dc:date>
    </item>
  </channel>
</rss>

