<?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: Problem with creating a counter variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396143#M95605</link>
    <description>&lt;P&gt;Your DATA step is fine. &amp;nbsp;Check your MASTER5 data. &amp;nbsp;It looks like your "macro" (whatever you mean by that) accidentally created a data set with 36 observations for each customer.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2017 04:04:11 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-09-15T04:04:11Z</dc:date>
    <item>
      <title>Problem with creating a counter variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396138#M95602</link>
      <description>&lt;P&gt;In my data set, I have a Month_Date field and a CustomerNo field.&amp;nbsp; I want to add a Counter field that starts from zero (0) for each Month_Date associated with a particular CustomerNo.&amp;nbsp; My macro defined a 36 month time frame but not all CustomerNo will have 36 observations.&amp;nbsp; Some might have 1 or 2 or 3 all the way up to 36.&amp;nbsp; This is how I want the output to appear:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MonthDate &amp;nbsp; &amp;nbsp; CustomerNo &amp;nbsp; &amp;nbsp; Counter&lt;/P&gt;
&lt;P&gt;201410 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 288 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;201411 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 288 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;201412 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 288 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;201501 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 288 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;201502 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 288 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 4&lt;/P&gt;
&lt;P&gt;201410 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 351 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;201411 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 351 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;201412 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 351 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, CustomerNo 288 has 5 observations and CustomerNo 351 has 3 observations.&amp;nbsp; The Counter should start from zero and end with the last given MonthDate for the respective CustomerNo.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how I put together my code in a data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data Final;&lt;/P&gt;
&lt;P&gt;set Master5;&lt;/P&gt;
&lt;P&gt;by CustomerNo;&lt;/P&gt;
&lt;P&gt;retain count;&lt;/P&gt;
&lt;P&gt;label count='Counter";&lt;/P&gt;
&lt;P&gt;keep MonthDate CustomerNo count;&lt;/P&gt;
&lt;P&gt;if first.CustomerNo then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;count = 0;&lt;/P&gt;
&lt;P&gt;else count = count + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, the output for this code generates a counter (and outputs each MonthDate) for each of the 36 months set in my original macro, even if there is no MonthDate record in the data set for that particular CustomerNo.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong in this code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance would be greatly appreciated.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 03:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396138#M95602</guid>
      <dc:creator>greg6363</dc:creator>
      <dc:date>2017-09-15T03:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with creating a counter variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396143#M95605</link>
      <description>&lt;P&gt;Your DATA step is fine. &amp;nbsp;Check your MASTER5 data. &amp;nbsp;It looks like your "macro" (whatever you mean by that) accidentally created a data set with 36 observations for each customer.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 04:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396143#M95605</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-15T04:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with creating a counter variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396210#M95631</link>
      <description>&lt;P&gt;Try this....&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  do i=1 to 12;
   format date date9.;
    date=mdy(i,i,2017);
drop i;
     output;
  end;
run;

data WANT;
  set 
  HAVE (in=a obs=3)
  HAVE (in=b firstobs=2 obs=8)
  HAVE (in=c firstobs=7)
  HAVE (IN=d obs=1);

if b then custno=2;
if c then custno=3;
if d then custno=4;
run;

proc sort;
  by custno date;
run;

data WANT;
  set WANT;
  by custno;
if first.custno then counter=0;
  else counter+1;
run;

 
     &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 08:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-counter-variable/m-p/396210#M95631</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-09-15T08:30:28Z</dc:date>
    </item>
  </channel>
</rss>

