<?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 How can I output to two different datasets and count separately in each? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857905#M37853</link>
    <description>&lt;P&gt;Hello and thank you in advance,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to output to two different datasets and use a counter variable in each data set that counts separately. Like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;data have; infile datalines dlm=','; input blah $ var1 var2 ; datalines; a, 1, 2,&amp;nbsp; b, 3, 2,&amp;nbsp; c, 3, 3, d, 8, 4,&amp;nbsp; e, 5, 6,&amp;nbsp; f, 7, 2, g, 3, 1,&amp;nbsp; h, 9, 4,&amp;nbsp; i, 8, 9 ; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want1 want2; set have; if blah in ('a', 'b', 'c', 'd') then do;&lt;BR /&gt;do i=1 to (total number of records but I don't know how to specify that);&lt;BR /&gt;recordno=i;&lt;BR /&gt;output want1;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;BR /&gt;do i=1 to total obs in that dataset;&lt;BR /&gt;recordno=i;&lt;BR /&gt;output want2;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the result that looks something like this:&lt;/P&gt;&lt;P&gt;Want 1 :&lt;/P&gt;&lt;P&gt;blah var1 var2 recordno&lt;/P&gt;&lt;P&gt;a&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;b&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;d&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want 2:&lt;/P&gt;&lt;P&gt;blah var1 var2 recordno&lt;/P&gt;&lt;P&gt;e&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;f&amp;nbsp; &amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;g&amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;h&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on.&lt;/P&gt;&lt;P&gt;How can I do this ?&lt;/P&gt;&lt;P&gt;I know there must be use of a do loop. I'm just not figuring out how to do so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
    <pubDate>Wed, 08 Feb 2023 23:26:26 GMT</pubDate>
    <dc:creator>JMagenta</dc:creator>
    <dc:date>2023-02-08T23:26:26Z</dc:date>
    <item>
      <title>How can I output to two different datasets and count separately in each?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857905#M37853</link>
      <description>&lt;P&gt;Hello and thank you in advance,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to output to two different datasets and use a counter variable in each data set that counts separately. Like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;data have; infile datalines dlm=','; input blah $ var1 var2 ; datalines; a, 1, 2,&amp;nbsp; b, 3, 2,&amp;nbsp; c, 3, 3, d, 8, 4,&amp;nbsp; e, 5, 6,&amp;nbsp; f, 7, 2, g, 3, 1,&amp;nbsp; h, 9, 4,&amp;nbsp; i, 8, 9 ; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want1 want2; set have; if blah in ('a', 'b', 'c', 'd') then do;&lt;BR /&gt;do i=1 to (total number of records but I don't know how to specify that);&lt;BR /&gt;recordno=i;&lt;BR /&gt;output want1;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;BR /&gt;do i=1 to total obs in that dataset;&lt;BR /&gt;recordno=i;&lt;BR /&gt;output want2;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the result that looks something like this:&lt;/P&gt;&lt;P&gt;Want 1 :&lt;/P&gt;&lt;P&gt;blah var1 var2 recordno&lt;/P&gt;&lt;P&gt;a&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;b&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;d&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want 2:&lt;/P&gt;&lt;P&gt;blah var1 var2 recordno&lt;/P&gt;&lt;P&gt;e&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;f&amp;nbsp; &amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;g&amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;h&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on.&lt;/P&gt;&lt;P&gt;How can I do this ?&lt;/P&gt;&lt;P&gt;I know there must be use of a do loop. I'm just not figuring out how to do so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 23:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857905#M37853</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-02-08T23:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I output to two different datasets and count separately in each?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857907#M37854</link>
      <description>Not sure a do loop is the correct approach here. That would replicate rows from your original data set. Please show what the original data set would look like and how you would decide which rows from the original go to the new data sets. If replicating, how do you decide how many times to replicate the row?</description>
      <pubDate>Wed, 08 Feb 2023 23:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857907#M37854</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-08T23:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I output to two different datasets and count separately in each?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857909#M37855</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Besides a Have data step that won't run as posted my concern is this&lt;BR /&gt;do i=1 to (total number of records but I don't know how to specify that);&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you don't know how many records or provide a rule for us, how can we know what to do?&lt;/P&gt;
&lt;P&gt;You should provide much more detail about what "and so on" actually means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also tell us what the resulting multiple data sets were to be used for. It may be, quite often, that splitting a data set is not needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A data step that will run that creates 9 records:&lt;/P&gt;
&lt;PRE&gt;data have; 
   infile datalines dlm=','; 
   input blah $ var1 var2 ; 
datalines;
a, 1, 2,
b, 3, 2, 
c, 3, 3, 
d, 8, 4,  
e, 5, 6,  
f, 7, 2, 
g, 3, 1,  
h, 9, 4,  
i, 8, 9 
; 
run;&lt;/PRE&gt;
&lt;P&gt;Here is a way to add a group variable that takes on the 1, 2, 3 etc in groups of 4 and adds a recordno variable per group.&lt;/P&gt;
&lt;PRE&gt;data have; 
   infile datalines dlm=','; 
   input blah $ var1 var2 ;
   retain group 0;
   recordno= mod(_n_,4); 
   if recordno=1 then group+1;
   if recordno=0 then recordno=4;
datalines;
a, 1, 2,
b, 3, 2, 
c, 3, 3, 
d, 8, 4,  
e, 5, 6,  
f, 7, 2, 
g, 3, 1,  
h, 9, 4,  
i, 8, 9 
; 
run;&lt;/PRE&gt;
&lt;P&gt;The data step DATA statement would require having all of the data sets Want1 to WantXX declared. If you do not know how many there should be then you cant write the data step to split the data at all.&lt;/P&gt;
&lt;P&gt;The above code would allow BY Group processing using the GROUP variable to do something with each set of 4 records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 00:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857909#M37855</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-09T00:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I output to two different datasets and count separately in each?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857921#M37856</link>
      <description>&lt;P&gt;So I don't want to group the datasets by 4. I just want to output each dataset and have a variable that numbers each observation in the dataset. Or counts the number of rows in the dataset. Just as long as it counts each dataset separately. In the code below I have to keep the variable "recordno" in the first column so I'm already using the retain function out of the gate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For this code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have; 
   infile datalines dlm=','; 
   input blah $ var1 var2 ;
datalines;
a, 1, 2,
b, 3, 2, 
c, 3, 3, 
d, 8, 4,  
e, 5, 6,  
f, 7, 2, 
g, 3, 1,  
h, 9, 4,  
i, 8, 9 
; 
run;


Data want1 want2;
retain recordno;
set have;
If blah in ('a', 'b', 'c') then output want1;
else output want2;
   recordno=_n_;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get the following for WANT 1:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_0-1675904763819.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80254i7C5DB6E060BF74A6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_0-1675904763819.png" alt="JMagenta_0-1675904763819.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I get the following for WANT2:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_1-1675904873542.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80255iFB509F01BCFD6684/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_1-1675904873542.png" alt="JMagenta_1-1675904873542.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What I would like to get for WANT 1 is continuous counting starting with 1, for each observation. I would like the same for WANT 2.&amp;nbsp; See the following.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want1:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_2-1675905212922.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80256iA895203ADF2870AA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_2-1675905212922.png" alt="JMagenta_2-1675905212922.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Want 2:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JMagenta_3-1675905600058.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80257iBAF0383F2271B74B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JMagenta_3-1675905600058.png" alt="JMagenta_3-1675905600058.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully this is clearer and that you can help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 01:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857921#M37856</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-02-09T01:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: How can I output to two different datasets and count separately in each?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857941#M37864</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438491"&gt;@JMagenta&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So I don't want to group the datasets by 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You want to be VERY careful about building your example data and cases to not imply rules. You had 9 input records in your original question, showed two sets with 4 records IN ORDER of appearance as the output data and did not display the 9th. So where did that ninth go? The implication without explicit rules was "select 4 records in order". You did not explain any rule for which records were to be selected to which data set, just a "sort of example".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, examine the Have set below. Note that the blah in ('a' 'b' 'c') are not in sequential order.&lt;/P&gt;
&lt;P&gt;Then parse the second set for splitting.&lt;/P&gt;
&lt;PRE&gt;data have; 
   infile datalines dlm=','; 
   input blah $ var1 var2 ;
datalines;
a, 1, 2,
e, 3, 2, 
c, 3, 3, 
d, 8, 4,  
b, 5, 6,  
f, 7, 2, 
a, 3, 1,  
h, 9, 4,  
c, 8, 9 
; 
run;

data want1 (rename=(rec1=recordno) drop=rec2)
     want2 (rename=(rec2=recordno) drop=rec1)
;
   retain rec1 rec2 0;

   set have;
   if blah in ('a' 'b' 'c') then do;
      rec1+1;
      output want1;
   end;
   else do;
      rec2+1;
      output want2;
   end;
run;
&lt;/PRE&gt;
&lt;P&gt;Hint: If you have TWO sequences then you want TWO variables to hold the count values. Conditionally increment the counter based on the stated selection rule(s).&lt;/P&gt;
&lt;P&gt;Rename to get the one variable name wanted and drop the other variable as output data set options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 06:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/857941#M37864</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-09T06:29:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I output to two different datasets and count separately in each?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/858099#M37891</link>
      <description>&lt;P&gt;Thank you! I had to make it work for a much larger dataset but it worked.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 17:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-can-I-output-to-two-different-datasets-and-count-separately/m-p/858099#M37891</guid>
      <dc:creator>JMagenta</dc:creator>
      <dc:date>2023-02-09T17:41:33Z</dc:date>
    </item>
  </channel>
</rss>

