<?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 Create count variable based on numeric date values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760981#M240717</link>
    <description>&lt;P&gt;I am trying to create a count variable based on a numeric date variable (format yyq6.), but I am unable to get what I want where each new date value is ennumerated&lt;/P&gt;&lt;P&gt;DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; TCOUNT&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;2014Q2&amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;2014Q2&amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code that I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.TEST1;&lt;BR /&gt;SET HAVE;&lt;BR /&gt;LABEL TCOUNT = "Time Count";&lt;BR /&gt;TCOUNT = 0;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SORT DATA = WORK.TEST1;&lt;BR /&gt;BY DATE ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.TEST1;&lt;BR /&gt;SET WORK.TEST1;&lt;BR /&gt;BY DATE;&lt;BR /&gt;IF FIRST.DATE THEN TCOUNT = TCOUNT + 1;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output is the following:&lt;/P&gt;&lt;P&gt;DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; TCOUNT&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;</description>
    <pubDate>Wed, 11 Aug 2021 18:27:42 GMT</pubDate>
    <dc:creator>pbhatt</dc:creator>
    <dc:date>2021-08-11T18:27:42Z</dc:date>
    <item>
      <title>Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760981#M240717</link>
      <description>&lt;P&gt;I am trying to create a count variable based on a numeric date variable (format yyq6.), but I am unable to get what I want where each new date value is ennumerated&lt;/P&gt;&lt;P&gt;DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; TCOUNT&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;2014Q2&amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;2014Q2&amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code that I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.TEST1;&lt;BR /&gt;SET HAVE;&lt;BR /&gt;LABEL TCOUNT = "Time Count";&lt;BR /&gt;TCOUNT = 0;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SORT DATA = WORK.TEST1;&lt;BR /&gt;BY DATE ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.TEST1;&lt;BR /&gt;SET WORK.TEST1;&lt;BR /&gt;BY DATE;&lt;BR /&gt;IF FIRST.DATE THEN TCOUNT = TCOUNT + 1;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output is the following:&lt;/P&gt;&lt;P&gt;DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; TCOUNT&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q2&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q3&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2013Q4&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014Q1&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 18:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760981#M240717</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-08-11T18:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760982#M240718</link>
      <description>&lt;P&gt;Have you tried putting a RETAIN on TCOUNT?&amp;nbsp; Or, alternatively, change it to TCOUNT + 1 instead of TCOUNT = TCOUNT + 1.&amp;nbsp; TCOUNT + 1 syntax causes an automatic retain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would also get rid of the Data step before the Sort.&amp;nbsp; Apply the label in the final step.&amp;nbsp; If you create TCOUNT before the final Data step, TCOUNT will not be a counter.&amp;nbsp; TCOUNT will get read in as zero each time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would fix it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	FORMAT	DATE	YYQ6.;
	INFILE DATALINES	TRUNCOVER;
	INPUT	DATE	:	YYQ6.;
DATALINES;
2013Q1
2013Q1
2013Q2
2013Q2
2013Q2
2013Q3
2013Q3
2013Q3
2013Q4
2013Q4
2014Q1
2014Q1
2014Q2
2014Q2
;
RUN;

PROC SORT DATA 		= 	Have
			OUT		=	WORK.TEST1;
	BY	DATE;
RUN;

DATA WORK.TEST1;
	SET WORK.TEST1;
		BY	DATE;

	LABEL TCOUNT	= 	"Time Count";

	IF FIRST.DATE 		THEN
		TCOUNT 		+ 	1;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1628709139759.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62483i0773B2BF08713304/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1628709139759.png" alt="jimbarbour_0-1628709139759.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 19:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760982#M240718</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-11T19:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760991#M240723</link>
      <description>I tried the following but it resulted in the same output. I also referenced this article but am still running into hurdles&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;DATA WORK.TEST1;&lt;BR /&gt;SET HAVE;&lt;BR /&gt;LABEL TCOUNT = "Time Count";&lt;BR /&gt;TCOUNT = 0;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC SORT DATA = WORK.TEST1;&lt;BR /&gt;BY DATE ;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;DATA WORK.TEST1;&lt;BR /&gt;SET WORK.TEST1;&lt;BR /&gt;RETAIN TCOUNT;&lt;BR /&gt;BY DATE;&lt;BR /&gt;IF FIRST.DATE THEN TCOUNT + 1;&lt;BR /&gt;RUN;</description>
      <pubDate>Wed, 11 Aug 2021 19:19:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760991#M240723</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-08-11T19:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760992#M240724</link>
      <description>&lt;P&gt;You are assigning the value zero to TCOUNT for every observation, but that is the problem. Also, RETAIN TCOUNT; is not wrong, but it is also not necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test1;
    set have;
    label tcount = "time count";
    /*tcount = 0;*/
run;

proc sort data = work.test1;
    by date ;
run;

data work.test1;
    set work.test1;
/*    retain tcount; */
    by date;
    if first.date then tcount + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thus, the code can be simplified to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data = have;
    by date ;
run;

data work.test1;
    set have;
    by date;
    if first.date then tcount + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 19:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760992#M240724</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-11T19:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760993#M240725</link>
      <description>&lt;P&gt;Read my response in detail.&amp;nbsp; I added an example after I initially posted it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This Data step is screwing things up.&amp;nbsp; It needs to be deleted.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.TEST1;
SET HAVE;
LABEL TCOUNT = "Time Count";
TCOUNT = 0;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 19:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760993#M240725</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-11T19:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760996#M240726</link>
      <description>Thanks so much to Paige and Jim! It worked. I was under the impression I needed to initialize TCOUNT by setting it to zero after I created its label, but that is not the case.</description>
      <pubDate>Wed, 11 Aug 2021 19:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760996#M240726</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-08-11T19:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create count variable based on numeric date values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760997#M240727</link>
      <description>&lt;P&gt;Yes, actually quite the opposite.&amp;nbsp; By adding TCOUNT to the dataset&amp;nbsp;&lt;EM&gt;before&lt;/EM&gt; the Data step counting TCOUNT, you caused TCOUNT to be read in each time the SET statement was executed, wiping out any count and replacing it with the initial value (zero).&amp;nbsp; You could have initialized TCOUNT in the&amp;nbsp;&lt;EM&gt;same&lt;/EM&gt; Data step as the counting, but it would not have been necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Glad we got you working,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 11 Aug 2021 19:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-count-variable-based-on-numeric-date-values/m-p/760997#M240727</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-11T19:52:06Z</dc:date>
    </item>
  </channel>
</rss>

