<?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 across dates - by a categorical variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518195#M140212</link>
    <description>This is wonderful!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thank-you again&lt;BR /&gt;&lt;BR /&gt;Michelle&lt;BR /&gt;</description>
    <pubDate>Mon, 03 Dec 2018 19:08:40 GMT</pubDate>
    <dc:creator>edwardsm</dc:creator>
    <dc:date>2018-12-03T19:08:40Z</dc:date>
    <item>
      <title>Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518155#M140188</link>
      <description>&lt;P&gt;Good day all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking for some help with a twist on a counter variable question.&amp;nbsp;&amp;nbsp;I have the following data - temperatures taken on each day of the year for a number of years.&amp;nbsp; I need to count the number of days the temperature is below 5, between 5-10, between 10-15, etc... My goal is to have a dataset that shows me what temperature&amp;nbsp;range (defined by a new variable called code) the observation falls in and how many days the temperature was within that given range.&amp;nbsp; So we might have a run where the temperature was below 5 for 10 days then between 5 and 10 for 2 days, and then back below 5 for another 3 days, etc....&amp;nbsp;&amp;nbsp;An example of the final dataset might be something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date Temp Code #days&lt;BR /&gt;01-Mar-15 -13.33&amp;nbsp; 1 1&lt;BR /&gt;02-Mar-15 -12.77&amp;nbsp; &amp;nbsp;1 2&lt;BR /&gt;03-Mar-15 -14.44&amp;nbsp; &amp;nbsp;1 3&lt;BR /&gt;04-Mar-15 -22.22&amp;nbsp; &amp;nbsp;1 4&lt;BR /&gt;05-Mar-15 -15.55&amp;nbsp; &amp;nbsp;1 5&lt;BR /&gt;06-Mar-15 -7.22&amp;nbsp; &amp;nbsp;1 6&lt;BR /&gt;07-Mar-15 -8.33&amp;nbsp; &amp;nbsp;1 7&lt;BR /&gt;08-Mar-15 -4.44&amp;nbsp; &amp;nbsp;1 8&lt;BR /&gt;09-Mar-15 -1.66&amp;nbsp; &amp;nbsp;1 9&lt;BR /&gt;10-Mar-15 3.33&amp;nbsp; &amp;nbsp;1 10&lt;BR /&gt;11-Mar-15 0&amp;nbsp; &amp;nbsp;1&amp;nbsp; 11&lt;BR /&gt;12-Mar-15 4.44&amp;nbsp; &amp;nbsp;1 12&lt;BR /&gt;13-Mar-15 3.33&amp;nbsp; &amp;nbsp;1 13&lt;BR /&gt;14-Mar-15 5.55&amp;nbsp; &amp;nbsp;2 1&lt;BR /&gt;15-Mar-15 9.44&amp;nbsp; &amp;nbsp;2 2&lt;BR /&gt;16-Mar-15 -1.11&amp;nbsp; &amp;nbsp;1 1&lt;BR /&gt;17-Mar-15 -1.11&amp;nbsp; &amp;nbsp;1 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, on March 8, the temperature was below 5 for 8 days, and on March 15, the temperature was between 5 and 10 for 2 days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought I could start by creating a new variable called "code" that would identify which&amp;nbsp;temperature range&amp;nbsp;each observation would fall into.&amp;nbsp; Then I tried using it to create my counter and well... that's when everything failed.&amp;nbsp; Most examples I've found use the sort, by, first. ...&amp;nbsp; and that doesn't work with my data, since I want to count the number of times a code appears in order of the dates.&amp;nbsp; I hope this makes sense and that someone can help me out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a snippet of the data and code so far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank-you!&lt;BR /&gt;Michelle&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input weather_date date10.&amp;nbsp; temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; datalines;&lt;/P&gt;&lt;P&gt;01-Mar-15 -13.33&lt;BR /&gt;02-Mar-15 -12.77&lt;BR /&gt;03-Mar-15 -14.44&lt;BR /&gt;04-Mar-15 -22.22&lt;BR /&gt;05-Mar-15 -15.55&lt;BR /&gt;06-Mar-15 -7.22&lt;BR /&gt;07-Mar-15 -8.33&lt;BR /&gt;08-Mar-15 -4.44&lt;BR /&gt;09-Mar-15 -1.66&lt;BR /&gt;10-Mar-15 3.33&lt;BR /&gt;11-Mar-15 0&lt;BR /&gt;12-Mar-15 4.44&lt;BR /&gt;13-Mar-15 3.33&lt;BR /&gt;14-Mar-15 5.55&lt;BR /&gt;15-Mar-15 9.44&lt;BR /&gt;16-Mar-15 -1.11&lt;BR /&gt;17-Mar-15 -1.11&lt;BR /&gt;18-Mar-15 0&lt;BR /&gt;19-Mar-15 3.33&lt;BR /&gt;20-Mar-15 -5.55&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; format weather_data date10.;&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data test2;&lt;BR /&gt;&amp;nbsp; set weather;&lt;BR /&gt;&amp;nbsp; if temp le 5 then code = 1;&lt;BR /&gt;&amp;nbsp; if temp &amp;gt; 5 and temp le 10 then code = 2;&lt;BR /&gt;&amp;nbsp; if temp &amp;gt; 10 and temp le 15 then code = 3;&lt;BR /&gt;&amp;nbsp; if temp &amp;gt; 15 and temp le 20 then code = 4;&lt;BR /&gt;&amp;nbsp; if temp &amp;gt; 20 then code = 5;&lt;BR /&gt;Run;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 17:14:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518155#M140188</guid>
      <dc:creator>edwardsm</dc:creator>
      <dc:date>2018-12-03T17:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518164#M140194</link>
      <description>&lt;P&gt;Create a new variable where the value of temperature is rounded to the nearest 5 degrees. Then do some counting&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data temp;

  input weather_date date10.  temp;
  temp_rounded=round(temp,5);
  prev_temp_rounded=lag(temp_rounded);
  if prev_temp_rounded^=temp_rounded then counter=0;
  else counter+1;
  datalines;
...
run;&lt;/CODE&gt;&lt;/PRE&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;
&lt;P&gt;&amp;nbsp;&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>Mon, 03 Dec 2018 17:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518164#M140194</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-12-03T17:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518171#M140197</link>
      <description>&lt;P&gt;Hi Michelle and&amp;nbsp;welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another suggestion:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set temp;
code=min(max(ceil(temp/5), 1), 5);
if dif(code) then days=1;
else days+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Dec 2018 18:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518171#M140197</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-12-03T18:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518175#M140200</link>
      <description>&lt;P&gt;Thank-you!!!&amp;nbsp; This worked like a charm!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a wonderful day and thank- you for the help!&lt;BR /&gt;Michelle&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 18:26:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518175#M140200</guid>
      <dc:creator>edwardsm</dc:creator>
      <dc:date>2018-12-03T18:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518176#M140201</link>
      <description>&lt;P&gt;Thank-you for helping out.&amp;nbsp; When I tried your proposed code it was close but the counter wasn't quite doing what I needed it to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Michelle&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 18:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518176#M140201</guid>
      <dc:creator>edwardsm</dc:creator>
      <dc:date>2018-12-03T18:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518192#M140211</link>
      <description>&lt;P&gt;You're welcome!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just in case you'll need more flexibility for the definition of CODE at some point&amp;nbsp;(e.g. with varying interval lengths so that a simple formula like&amp;nbsp;&lt;FONT face="courier new,courier"&gt;ceil(temp/5)&lt;/FONT&gt; would no longer work), you can also create and use a format. The example below uses your current definition.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value tempf
low -  5  = '1'
  5&amp;lt;- 10  = '2'
 10&amp;lt;- 15  = '3'
 15&amp;lt;- 20  = '4'
 20&amp;lt;-high = '5';
run;

data want(drop=_t);
set temp(rename=(temp=_t));
by _t groupformat notsorted;
format _t tempf.;
temp=_t;
code=input(put(_t, tempf.), 8.);
if first._t then days=1;
else days+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 19:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518192#M140211</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-12-03T19:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a counter across dates - by a categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518195#M140212</link>
      <description>This is wonderful!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thank-you again&lt;BR /&gt;&lt;BR /&gt;Michelle&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Dec 2018 19:08:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-counter-across-dates-by-a-categorical-variable/m-p/518195#M140212</guid>
      <dc:creator>edwardsm</dc:creator>
      <dc:date>2018-12-03T19:08:40Z</dc:date>
    </item>
  </channel>
</rss>

