<?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: Counting in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431977#M106912</link>
    <description>&lt;P&gt;All three proposed solutions worked. This one is the easiest to understand though. Thanks a lot everyone for their help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Recep&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jan 2018 21:25:33 GMT</pubDate>
    <dc:creator>Recep</dc:creator>
    <dc:date>2018-01-29T21:25:33Z</dc:date>
    <item>
      <title>Counting in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431493#M106745</link>
      <description>&lt;P&gt;Hello there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to count the number of visits by each client within the data step for the following data (input) and desired outcome (output):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data input;&lt;BR /&gt;input client $ visit $;&lt;BR /&gt;datalines;&lt;BR /&gt;1 .&lt;BR /&gt;2 .&lt;BR /&gt;2 Y&lt;BR /&gt;3 Y&lt;BR /&gt;4 .&lt;BR /&gt;5 .&lt;BR /&gt;5 Y&lt;BR /&gt;5 .&lt;BR /&gt;5 Y&lt;BR /&gt;5 .&lt;BR /&gt;6 Y&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data output;&lt;BR /&gt;input client $ visit $ count;&lt;BR /&gt;datalines;&lt;BR /&gt;1 . 0&lt;BR /&gt;2 . 0&lt;BR /&gt;2 Y 1&lt;BR /&gt;3 Y 1&lt;BR /&gt;4 . 0&lt;BR /&gt;5 . 0&lt;BR /&gt;5 Y 1&lt;BR /&gt;5 . 2&lt;BR /&gt;5 Y 3&lt;BR /&gt;5 . 4&lt;BR /&gt;6 Y 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Normally counting the number of visits by each client would be relatively simple task for me with the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=input;by client;&lt;/P&gt;&lt;P&gt;data output;&lt;BR /&gt;set input;&lt;/P&gt;&lt;P&gt;by client;&lt;/P&gt;&lt;P&gt;if first.client then count=1;&lt;BR /&gt;else count+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, one complication I would like to add is &lt;U&gt;to start counting&lt;/U&gt; when there is a "Y" for the client. For instance Client 1 does not have a "Y" in the&amp;nbsp;VISIT column so the count for him/her will start with 0 and end with 0. Client 2 has a "Y" in his/her second visit so the count will start from his/her first visit as 0 and second visit as 1. Client 5 does not have a "Y" in his/her first visit and has a "Y" in his/her second visit so the count will start from the second visit as 1. Even though his/her 3rd visit does not have a "Y" just because we started to count in the second visit we will continue to count until we see a new client.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope I was clear in my explanation and would like to thank in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Recep&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jan 2018 01:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431493#M106745</guid>
      <dc:creator>Recep</dc:creator>
      <dc:date>2018-01-27T01:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431499#M106750</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input;
input client $ visit $;
datalines;
1 .
2 .
2 Y
3 Y
4 .
5 .
5 Y
5 .
5 Y
5 .
6 Y
;
run;

data want;
if 0 then set input;
count=0;
do until(last.client);
set input;
by client;
if visit='Y' then do; _t=1;count+1;end;
else if _t=1 then count+1;
output;
end;&lt;BR /&gt;drop _t;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Jan 2018 01:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431499#M106750</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-27T01:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431510#M106754</link>
      <description>&lt;P&gt;Simple once you think of it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by client;&lt;/P&gt;
&lt;P&gt;if first.client then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;count=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;increment=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;retain increment;&lt;/P&gt;
&lt;P&gt;if visit='Y' then increment=1;&lt;/P&gt;
&lt;P&gt;count + increment;&lt;/P&gt;
&lt;P&gt;drop increment;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jan 2018 02:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431510#M106754</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-27T02:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431514#M106755</link>
      <description>&lt;P&gt;Reduced to its core, you want to increment count once the first visit='Y' record has been encountered for a given id:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data output;
  set input;
  by client;
  if first.client then count=0;
  if count&amp;gt;0 or visit='Y' then count+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Jan 2018 04:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431514#M106755</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-01-27T04:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Counting in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431977#M106912</link>
      <description>&lt;P&gt;All three proposed solutions worked. This one is the easiest to understand though. Thanks a lot everyone for their help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Recep&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 21:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-in-data-step/m-p/431977#M106912</guid>
      <dc:creator>Recep</dc:creator>
      <dc:date>2018-01-29T21:25:33Z</dc:date>
    </item>
  </channel>
</rss>

