<?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: by process to get transition count in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/469152#M70789</link>
    <description>&lt;P&gt;To calculate the number of times each light has been switched on, you can do something like this:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; n_switch_on=0;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do until(last.light_no);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by Light_no on_off_status notsorted;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_switch_on+first.on_off_status and on_off_status='ON';&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; keep light_no n_switch_on;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unless, of course, you do not want to count if the light is on in the first hour, then the last line in the loop&amp;nbsp;should be&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_switch_on+first.on_off_status and not first.light_no and on_off_status='ON';&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jun 2018 10:03:31 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-06-11T10:03:31Z</dc:date>
    <item>
      <title>by process to get transition count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468877#M70774</link>
      <description>&lt;P&gt;how to get the count of swtiched ON by light by day; I have a dataset with a light no.. I need get a count of how many times the light is switched on in the set.&amp;nbsp; for example, I want to return the results that the light_no. 1 was swtiched on 2 times in the dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Light_no.&amp;nbsp; &amp;nbsp;HOUR ON_OFF_STATUS&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OFF&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OFF&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ON&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ON&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; OFF&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ON&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this is data step I am using. getting weird results. Any idea?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data ct;&lt;/P&gt;
&lt;P&gt;set temp;&lt;/P&gt;
&lt;P&gt;where ON_OFF_STATUS = ON;&lt;/P&gt;
&lt;P&gt;by light_no. ON_OFF_STATUS notsorted;&lt;/P&gt;
&lt;P&gt;retain ct;&lt;/P&gt;
&lt;P&gt;if first. light_no. then do; ct+1; output&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if last.ON_OFF_STATUS then do;&lt;/P&gt;
&lt;P&gt;ct = .;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jun 2018 21:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468877#M70774</guid>
      <dc:creator>YW_CA</dc:creator>
      <dc:date>2018-06-08T21:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: by process to get transition count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468884#M70775</link>
      <description>&lt;P&gt;I think you should be retaining the previous state of the switch and then counting when the previous state is OFF and the current state is ON.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jun 2018 22:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468884#M70775</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2018-06-08T22:28:41Z</dc:date>
    </item>
    <item>
      <title>Re: by process to get transition count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468898#M70777</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/34689"&gt;@YW_CA&lt;/a&gt;&amp;nbsp; &amp;nbsp;In your sample it's been ON 3 times and not 2 times right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If yes,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Light_no   HOUR ON_OFF_STATUS $;
cards;
1                  1         OFF
1                  2         OFF
1                  3         ON
1                  4         ON
1                  5        OFF
1                  6        ON 
;

proc freq data=have noprint;
by Light_no;
where ON_OFF_STATUS='ON';
tables ON_OFF_STATUS/out=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Jun 2018 00:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468898#M70777</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-09T00:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: by process to get transition count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468899#M70778</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I'm not clear on your logic. You say that the light is only ON 2 times, but just counting, I see this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="on_off.png" style="width: 435px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21121i58139D9C0D5B3CA5/image-size/large?v=v2&amp;amp;px=999" role="button" title="on_off.png" alt="on_off.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;where ON is the value for hour 3, 4 and 6. Is there a reason why you're not counting hour 6?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would tend to use PROC TABULATE or PROC FREQ, if all you need is a report.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jun 2018 00:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468899#M70778</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-06-09T00:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: by process to get transition count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468925#M70780</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Light_no   HOUR ON_OFF_STATUS $;
cards;
1                  1         OFF
1                  2         OFF
1                  3         ON
1                  4         ON
1                  5        OFF
1                  6        ON 
;
data _null_;
 set have;
 by  Light_no ON_OFF_STATUS notsorted;
 if first.Light_no  then count=0;
 count+first.ON_OFF_STATUS;
 if last.Light_no then do;
  _count=count-1;
  put 'Light_no ' Light_no  ' has ' _count ' switch';
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Jun 2018 10:11:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/468925#M70780</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-06-09T10:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: by process to get transition count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/469152#M70789</link>
      <description>&lt;P&gt;To calculate the number of times each light has been switched on, you can do something like this:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; n_switch_on=0;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do until(last.light_no);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by Light_no on_off_status notsorted;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_switch_on+first.on_off_status and on_off_status='ON';&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; keep light_no n_switch_on;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unless, of course, you do not want to count if the light is on in the first hour, then the last line in the loop&amp;nbsp;should be&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_switch_on+first.on_off_status and not first.light_no and on_off_status='ON';&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 10:03:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/by-process-to-get-transition-count/m-p/469152#M70789</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-06-11T10:03:31Z</dc:date>
    </item>
  </channel>
</rss>

