<?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: Creating a counter that resets at a new subject ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264245#M51813</link>
    <description>&lt;P&gt;Is PART_RDATE the same for all observations that have the same FB_ID?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At any rate, you may need week calculations for some other purpose. &amp;nbsp;But for measuring first year vs. second year, you can skip most of the calculations. &amp;nbsp;For example, if you are willing to approximate a year as being 365 days and ignore leap years:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;days_passed = datepart(BH_Accomplished_date) - Part_RDate;&lt;/P&gt;
&lt;P&gt;if (0 &amp;lt; days_passed &amp;lt;= 365) then account_comp_yr1 + 1;&lt;/P&gt;
&lt;P&gt;else if (366 &amp;lt;= days_passed &amp;lt;= 730) then account_comp_yr2 + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are details to take care of, like resetting your counts when you begin a new FB_ID. &amp;nbsp;But you don't need to convert to weeks or years in order to increment your counters.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Apr 2016 18:39:17 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-04-15T18:39:17Z</dc:date>
    <item>
      <title>Creating a counter that resets at a new subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264229#M51806</link>
      <description>&lt;P&gt;I am trying to create a counter variable, two actually, that counts the number of accomplishments in year 1 and year 2 per subject ID (FB_ID). I want the counter to reset when it moves to a new subject ID. The ultimate goal is to be able to look at the number of accomplishments in year 1 and year 2 for each subject ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The dataset (attached) contains the randomization date and the date of the accomplishment, among other things. I am calculating the difference between the two, and then converting to weeks, in order to produce a variable that contains the number of weeks since randomization that the accomplishment occurred (i.e.&amp;nbsp;ACCOMP_WKS).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am then separating into year 1 and 2 and using a SUM statement to create the counter variables (i.e.&amp;nbsp;ACCOMP_COUNT_YR1,&amp;nbsp;ACCOMP_COUNT_YR2).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is just my attempt to get into the correct&amp;nbsp;neighborhood. I have never created a counter before.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my initial approach. It resets correctly, but the counts don't seem to be correct. Instead of 1,2,3, etc. I see 1,9,2, for example.&lt;/P&gt;
&lt;P&gt;Thanks for your help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
 SET have;

 ACCOMP_WKS = YRDIF(Part_RDATE, datepart(BH_Accomplished_Date), "Actual")*52;
 * Creating a variable - ACCOMP_WKS - that equals the number of years between RDATE and BH_Accomplished_Date;
 * BH_Accomplished_Date contains date and time. Using the DATEPART function to exclude the time, so that the YRDIF function can work;
 * Converting to weeks by multiplying by 52;
 
 IF ACCOMP_WKS le 52 THEN ACCOMP_COUNT_YR1 + 1;
 BY FB_ID;
 IF first.FB_ID THEN ACCOMP_COUNT_YR1=1;
 
 IF ACCOMP_WKS gt 52 THEN ACCOMP_COUNT_YR2 + 1; 
 IF first.FB_ID THEN ACCOMP_COUNT_YR2=1; 
 RUN;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Note. The code above was changed at 11:00am PST.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2016 18:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264229#M51806</guid>
      <dc:creator>_maldini_</dc:creator>
      <dc:date>2016-04-15T18:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter that resets at a new subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264238#M51811</link>
      <description>&lt;P&gt;Consider using intck with weeks to get interval instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a a year variable and add that to your by condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try getting a counter to work without weeks condition first.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2016 18:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264238#M51811</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-15T18:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter that resets at a new subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264245#M51813</link>
      <description>&lt;P&gt;Is PART_RDATE the same for all observations that have the same FB_ID?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At any rate, you may need week calculations for some other purpose. &amp;nbsp;But for measuring first year vs. second year, you can skip most of the calculations. &amp;nbsp;For example, if you are willing to approximate a year as being 365 days and ignore leap years:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;days_passed = datepart(BH_Accomplished_date) - Part_RDate;&lt;/P&gt;
&lt;P&gt;if (0 &amp;lt; days_passed &amp;lt;= 365) then account_comp_yr1 + 1;&lt;/P&gt;
&lt;P&gt;else if (366 &amp;lt;= days_passed &amp;lt;= 730) then account_comp_yr2 + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are details to take care of, like resetting your counts when you begin a new FB_ID. &amp;nbsp;But you don't need to convert to weeks or years in order to increment your counters.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2016 18:39:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264245#M51813</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-04-15T18:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter that resets at a new subject ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264299#M51824</link>
      <description>&lt;P&gt;Use INTCK and summarize with SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select 
    FB_ID, 
    sum(intck("Year",part_RDATE, BH_Accomplished_Date, "CONTINUOUS") = 0) 
        as ACCOMP_COUNT_YR1,
    sum(intck("Year",part_RDATE, BH_Accomplished_Date, "CONTINUOUS") = 1) 
        as ACCOMP_COUNT_YR2
from have
group by FB_ID;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Apr 2016 21:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-that-resets-at-a-new-subject-ID/m-p/264299#M51824</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-15T21:54:37Z</dc:date>
    </item>
  </channel>
</rss>

