<?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: Calculate a metric from time series data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808085#M318636</link>
    <description>&lt;P&gt;A very powerful tool in SAS is the custom format. You can create ranges that will be used by Reporting or Analysis procedures to make groups. Most of the graphing procedures will honor them as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of creating a data set with data step code (which the preferred way to show data as we can test code against such a data set when we run it). Then create a custom format and demonstrate how Proc Freq will generate the counts, running total, percent of values and running percent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input minutes Uv;
datalines;
1	0.229528
2	0.262015
3	0.06782
4	1.263749
5	2.969982
6	2.831766
7	1.749711
8	0.164038
9	0.685758
10	0.924174
11	0.150946
12	0.384327
13	1.135237
14	1.9148
15	1.039687
;

proc format;
value uvrange
low -&amp;lt;1 = 'uV&amp;lt;1'
1 - 2   = 'uV 1 to 2'
2&amp;lt;-high = 'uV&amp;gt;2'
;
proc freq data=have;
  tables uv;
  format uv uvrange.;
run;
data want;
   set have;
   Low = (uv &amp;lt; 1);
   Mid = (1 le uv le 2)l
   high&lt;/PRE&gt;
&lt;P&gt;One of the very powerful advantages of a format is that when the boss asks "what about interval boundaries at 1.5 and 2.5?" or anything similar. The only requirement would the create a new format and use that with the Prod Freq code instead of this Uvrange format.&lt;/P&gt;
&lt;P&gt;Note that this works for just about anything where groups ONLY depend on values of a single variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data had another variable, such as the machine or operator that made the recording you could include that variable on the tables statement to get the counts and percentages for each machine or operator (or test sequence or other identifier like this:&lt;/P&gt;
&lt;PRE&gt;proc freq data=have;
  tables uv * operatorid;
  format uv uvrange.;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Apr 2022 19:44:49 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-04-15T19:44:49Z</dc:date>
    <item>
      <title>Calculate a metric from time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808083#M318634</link>
      <description>&lt;P&gt;Hi Everybody!&lt;BR /&gt;&lt;BR /&gt;I have a question on some data. I am measuring potential (V) at minute intervals, and I want to know the duration (and percentage) of time that this potential is within a certain range.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if the data looked like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Time (min)&lt;/TD&gt;&lt;TD&gt;V (uV)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0.229528&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0.262015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0.06782&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1.263749&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2.969982&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2.831766&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;1.749711&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;0.164038&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;0.685758&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;0.924174&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;0.150946&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;0.384327&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;1.135237&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;1.9148&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;1.039687&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I calculate the amount of time that the variable V is:&lt;/P&gt;&lt;P&gt;1) &amp;lt; 1 uV&lt;/P&gt;&lt;P&gt;2) in the range 1-2 uV (including 1 and 2 uV)&lt;/P&gt;&lt;P&gt;3) &amp;gt; 2 uV&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The sum of these three times should also add to make 15 minutes as well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried some basic code but I would really appreciate peoples help!&lt;BR /&gt;&lt;BR /&gt;Kind regards,&lt;BR /&gt;Dion&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 19:23:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808083#M318634</guid>
      <dc:creator>serenedion</dc:creator>
      <dc:date>2022-04-15T19:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a metric from time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808085#M318636</link>
      <description>&lt;P&gt;A very powerful tool in SAS is the custom format. You can create ranges that will be used by Reporting or Analysis procedures to make groups. Most of the graphing procedures will honor them as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of creating a data set with data step code (which the preferred way to show data as we can test code against such a data set when we run it). Then create a custom format and demonstrate how Proc Freq will generate the counts, running total, percent of values and running percent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input minutes Uv;
datalines;
1	0.229528
2	0.262015
3	0.06782
4	1.263749
5	2.969982
6	2.831766
7	1.749711
8	0.164038
9	0.685758
10	0.924174
11	0.150946
12	0.384327
13	1.135237
14	1.9148
15	1.039687
;

proc format;
value uvrange
low -&amp;lt;1 = 'uV&amp;lt;1'
1 - 2   = 'uV 1 to 2'
2&amp;lt;-high = 'uV&amp;gt;2'
;
proc freq data=have;
  tables uv;
  format uv uvrange.;
run;
data want;
   set have;
   Low = (uv &amp;lt; 1);
   Mid = (1 le uv le 2)l
   high&lt;/PRE&gt;
&lt;P&gt;One of the very powerful advantages of a format is that when the boss asks "what about interval boundaries at 1.5 and 2.5?" or anything similar. The only requirement would the create a new format and use that with the Prod Freq code instead of this Uvrange format.&lt;/P&gt;
&lt;P&gt;Note that this works for just about anything where groups ONLY depend on values of a single variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data had another variable, such as the machine or operator that made the recording you could include that variable on the tables statement to get the counts and percentages for each machine or operator (or test sequence or other identifier like this:&lt;/P&gt;
&lt;PRE&gt;proc freq data=have;
  tables uv * operatorid;
  format uv uvrange.;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 19:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808085#M318636</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-15T19:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a metric from time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808090#M318640</link>
      <description>&lt;P&gt;Thank you for your quick reply ballardw! It is really useful. I see know how you have applied a format to set the variable as low high or medium, and then run a proc freq of this new formatted variable.&lt;/P&gt;&lt;P&gt;Can I just check, what does the last bit of code do? Specifically:&lt;/P&gt;&lt;PRE&gt;data want;
   set have;
   Low = (uv &amp;lt; 1);
   Mid = (1 le uv le 2)l
   high&lt;/PRE&gt;&lt;P&gt;Does this code calculate the length of time that uV is in each range (rather than the number of points in the variable uV that are Low, Mid or High)? For this, could I add minutes to proc freq?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 20:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808090#M318640</guid>
      <dc:creator>serenedion</dc:creator>
      <dc:date>2022-04-15T20:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a metric from time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808092#M318642</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/423070"&gt;@serenedion&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your quick reply ballardw! It is really useful. I see know how you have applied a format to set the variable as low high or medium, and then run a proc freq of this new formatted variable.&lt;/P&gt;
&lt;P&gt;Can I just check, what does the last bit of code do? Specifically:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   Low = (uv &amp;lt; 1);
   Mid = (1 le uv le 2)l
   high&lt;/PRE&gt;
&lt;P&gt;Does this code calculate the length of time that uV is in each range (rather than the number of points in the variable uV that are Low, Mid or High)? For this, could I add minutes to proc freq?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;typo. Started to create new variables. The code would have created 3 variables that were 1/0 coded. Just forgot to remove it.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 20:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808092#M318642</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-15T20:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a metric from time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808094#M318643</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/423070"&gt;@serenedion&lt;/a&gt;&amp;nbsp;wrote:
&lt;P class="1650056749565"&gt;&amp;nbsp;&lt;/P&gt;
Does this code calculate the length of time that uV is in each range (rather than the number of points in the variable uV that are Low, Mid or High)? For this, could I add minutes to proc freq?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You indicated each point was a minute.&amp;nbsp; So 5 points is 5 minutes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you also wanting "streaks"?&amp;nbsp; Your data would have "streaks" of 3,1,2,1,5, and 3 presumeably?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 21:10:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808094#M318643</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2022-04-15T21:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a metric from time series data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808177#M318664</link>
      <description>Hi HB, thanks for getting back to me. It took a while to work out what you meant!! But yes, I guess that could be a useful metric to report aswell. How would you identify and calculate these different streaks? Could you assign to each streak a duration? The other issue I have is that some of my data points are missing, so how would I handle this?</description>
      <pubDate>Sat, 16 Apr 2022 15:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-metric-from-time-series-data/m-p/808177#M318664</guid>
      <dc:creator>serenedion</dc:creator>
      <dc:date>2022-04-16T15:25:00Z</dc:date>
    </item>
  </channel>
</rss>

