<?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: Average sample data if it lays between 2 dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/721743#M223737</link>
    <description>&lt;P&gt;Average sample of what?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What type of variables are your start, end and datetime? Numeric with datetime format applied or character? One cannot tell from a picture.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please do not call values with a time component a "date" when discussing SAS variables. Dates and times use different units of measurement and when you say "date" that means days. And they don't compare well with time plus raising a question of are the "start" and "end" supposed to be considering the "date" portion or the actual datetime value the picture implies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And since "bed" is involved per "What I want to achieve is an averaged sample value for each bed." then which "bed" is the "sample" from? Or are we supposed to guess which bed was measured?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe something along these lines but completely untested as I can't write code to read pictures and you didn't provide names of data sets.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Feb 2021 02:52:04 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-02-25T02:52:04Z</dc:date>
    <item>
      <title>Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/721732#M223730</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First question here.&lt;/P&gt;&lt;P&gt;I have two datasets, the first containing a sample result column and a datetime column (when the sample was taken). The other, Containing a bed number, start date and end date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to achieve is an averaged sample value for each bed.&lt;/P&gt;&lt;P&gt;i.e. average the sample results if they lay between the start and end dates of each bed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture1.PNG" style="width: 285px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55140i1188D809956940D6/image-dimensions/285x334?v=v2" width="285" height="334" role="button" title="Capture1.PNG" alt="Capture1.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 278px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55141i14E84EF98B4F410C/image-dimensions/278x662?v=v2" width="278" height="662" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 02:01:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/721732#M223730</guid>
      <dc:creator>ZacLopresti</dc:creator>
      <dc:date>2021-02-25T02:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/721743#M223737</link>
      <description>&lt;P&gt;Average sample of what?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What type of variables are your start, end and datetime? Numeric with datetime format applied or character? One cannot tell from a picture.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please do not call values with a time component a "date" when discussing SAS variables. Dates and times use different units of measurement and when you say "date" that means days. And they don't compare well with time plus raising a question of are the "start" and "end" supposed to be considering the "date" portion or the actual datetime value the picture implies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And since "bed" is involved per "What I want to achieve is an averaged sample value for each bed." then which "bed" is the "sample" from? Or are we supposed to guess which bed was measured?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe something along these lines but completely untested as I can't write code to read pictures and you didn't provide names of data sets.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 02:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/721743#M223737</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-25T02:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722050#M223850</link>
      <description>&lt;P&gt;I presume you want the weighted average PERCENTGRIND for each bed (from dataset BEDS) where weights are determined by duration of the bed used within each period identified in your PCT_GRIND dataset (that's the name I assign it).&amp;nbsp; I also presume that the PCT_GRIND date/time stamps are the start of a given level of PERCENTGRIND.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Luckily (1) your bed usages do not overlap&amp;nbsp; (each END precedes the next START in the dataset (which I call BEDS),&amp;nbsp; and (2) both datasets are sorted chronologically (BEDS is sorted by START, and PCT_GRIND is sorted by DATETIME).&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;These features are assumed to this program.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Getting a weighted average is straightforward using PROC SUMMARY (also know as PROC MEANS).&amp;nbsp; The problem is generating a dataset with the weights (variable DURATION in data set NEED):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need;
  set beds ;

  /* Exhaust PCT_GRIND periods that cover this START-END period */
  do until (datetime&amp;gt;end);
    if (datetime&amp;lt;=start&amp;lt;=nxt_dt)  or (datetime&amp;lt;=end&amp;lt;=nxt_dt) then do;
      duration =  1 + min(end,nxt_dt) - max(start,datetime);
      output;
    end;
    if nxt_dt&amp;gt;end then leave;  ** Read no more PCT_GRIND for this bed**;
    merge pct_grind
          pct_grind (firstobs=2 keep=datetime rename=(datetime=nxt_dt));
    format duration time10.0;
  end;
run;

proc summary data=need noprint;
  class bed;
  var percentgrind;  
  weight duration;   
  ways 1;
  output out=want mean=pctgrind_mean sumwgt=total_duration / autoname;
  format total_duration time10.0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For each bed, dataset NEED gets as many records as there are overlaps between the bed usage (START to END) with the time spans in PCT_GRIND (DATETIME to NXT_DT).&amp;nbsp; Each such record gets a variable called DURATION. That's where the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;duration =  1 + min(end,nxt_dt) - max(start,datetime);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;comes in.&amp;nbsp; Note the NXT_DT is the datetime starting the next PCT_GRIND span.&amp;nbsp; It is obtained by use of the "FIRSTOBS=2" parameter in the self-merge activated by the MERGE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since PERCENTGRIND is missing for some time spans, any bed use that includes that time span will include only the neighboring non-missing spans that overlaps with bed usage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also any bed usage that is outside of the range of times in PCT_GRIND will not be processed at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I recommend you examine dataset NEED to see the intent of the data step programming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 06:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722050#M223850</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-26T06:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722766#M224160</link>
      <description>&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe a better way of explaining is as follows.&lt;/P&gt;&lt;P&gt;I have a bed (which is a mix of minerals), during the time that a specific bed is being used, multiple samples of the size fraction (PercentGrind)&amp;nbsp;of the&amp;nbsp;bed are taken. I want to average all the sample results taken within a specific bed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the average of all the PercentGrind values if the corresponding DateTimes fall between the Startand End DateTimes of a specific Bed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To answer your questions:&lt;/P&gt;&lt;P&gt;- All datetime variables are numeric.&lt;/P&gt;&lt;P&gt;-Each sample has the DateTime at which it was taken recorded with it. If the DateTime is between the Start and End Datetimes it has been taken from that bed.&lt;/P&gt;&lt;P&gt;- I will try and upload the data not as oictures but had troubles in the past.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Zac&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 02:27:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722766#M224160</guid>
      <dc:creator>ZacLopresti</dc:creator>
      <dc:date>2021-03-02T02:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722768#M224162</link>
      <description>&lt;P&gt;data WORK.BATTERYFEEDSAMPLE;&lt;/P&gt;&lt;P&gt;infile datalines dsd truncover;&lt;/P&gt;&lt;P&gt;input DateTime:DATETIME20. PercentGrind:6.1;&lt;/P&gt;&lt;P&gt;format DateTime DATETIME20. PercentGrind 6.1;&lt;/P&gt;&lt;P&gt;label DateTime="SamplingStartTime" PercentGrind="Grind (%)";&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;25JUN2016:13:00:00 89.9&lt;/P&gt;&lt;P&gt;26JUN2016:01:00:00 89.8&lt;/P&gt;&lt;P&gt;26JUN2016:13:00:00 87.0&lt;/P&gt;&lt;P&gt;27JUN2016:13:00:00 90.3&lt;/P&gt;&lt;P&gt;28JUN2016:13:00:00 90.6&lt;/P&gt;&lt;P&gt;29JUN2016:01:00:00 90.9&lt;/P&gt;&lt;P&gt;29JUN2016:01:01:00 .&lt;/P&gt;&lt;P&gt;29JUN2016:13:00:00 89.8&lt;/P&gt;&lt;P&gt;30JUN2016:01:00:00 90.2&lt;/P&gt;&lt;P&gt;30JUN2016:13:00:00 89.6&lt;/P&gt;&lt;P&gt;01JUL2016:01:00:00 90.6&lt;/P&gt;&lt;P&gt;01JUL2016:13:00:00 86.1&lt;/P&gt;&lt;P&gt;02JUL2016:01:00:00 86.9&lt;/P&gt;&lt;P&gt;02JUL2016:13:00:00 86.1&lt;/P&gt;&lt;P&gt;03JUL2016:01:00:00 86.1&lt;/P&gt;&lt;P&gt;03JUL2016:13:00:00 84.7&lt;/P&gt;&lt;P&gt;04JUL2016:01:00:00 88.0&lt;/P&gt;&lt;P&gt;04JUL2016:13:00:00 89.7&lt;/P&gt;&lt;P&gt;05JUL2016:01:00:00 89.4&lt;/P&gt;&lt;P&gt;05JUL2016:13:00:00 89.6&lt;/P&gt;&lt;P&gt;06JUL2016:01:00:00 86.8&lt;/P&gt;&lt;P&gt;06JUL2016:01:01:00 .&lt;/P&gt;&lt;P&gt;06JUL2016:13:00:00 87.8&lt;/P&gt;&lt;P&gt;07JUL2016:01:00:00 89.4&lt;/P&gt;&lt;P&gt;07JUL2016:13:00:00 89.8&lt;/P&gt;&lt;P&gt;08JUL2016:01:00:00 89.9&lt;/P&gt;&lt;P&gt;08JUL2016:13:00:00 89.8&lt;/P&gt;&lt;P&gt;09JUL2016:01:00:00 90.0&lt;/P&gt;&lt;P&gt;;;;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data WORK.BED_DATETIME;&lt;/P&gt;&lt;P&gt;infile datalines dsd truncover;&lt;/P&gt;&lt;P&gt;input Bed:4. Start:DATETIME20. End:DATETIME20.;&lt;/P&gt;&lt;P&gt;format Bed 4. Start DATETIME20. End DATETIME20.;&lt;/P&gt;&lt;P&gt;label Bed="Bed";&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1668 26JUN2016:02:22:00 02JUL2016:03:29:59&lt;/P&gt;&lt;P&gt;1669 02JUL2016:03:30:00 08JUL2016:19:55:59&lt;/P&gt;&lt;P&gt;1670 08JUL2016:19:56:00 15JUL2016:20:18:59&lt;/P&gt;&lt;P&gt;1671 15JUL2016:20:19:00 22JUL2016:17:14:59&lt;/P&gt;&lt;P&gt;1672 22JUL2016:17:15:00 29JUL2016:14:26:59&lt;/P&gt;&lt;P&gt;1673 29JUL2016:14:27:00 05AUG2016:11:11:59&lt;/P&gt;&lt;P&gt;1674 05AUG2016:11:12:00 12AUG2016:13:41:59&lt;/P&gt;&lt;P&gt;;;;;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 02:43:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722768#M224162</guid>
      <dc:creator>ZacLopresti</dc:creator>
      <dc:date>2021-03-02T02:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722771#M224163</link>
      <description>&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;I want all samples to be equally weight though (regardless of time).&lt;/P&gt;&lt;P&gt;I'll have a play with what you've sent and see if I can modify it.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 02:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722771#M224163</guid>
      <dc:creator>ZacLopresti</dc:creator>
      <dc:date>2021-03-02T02:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722775#M224166</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/370971"&gt;@ZacLopresti&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your reply.&lt;/P&gt;
&lt;P&gt;I want all samples to be equally weight though (regardless of time).&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then drop the WEIGHT DURATION statement in the proc summary.&amp;nbsp; &amp;nbsp;And if you are never going to examine the exposure time to each PERCENTGRIND, you can also drop the calculation of DURATION in the DATA NEED step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 03:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722775#M224166</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-02T03:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722776#M224167</link>
      <description>&lt;P&gt;For equal weighting, it is quite simple, as long as bed periods don't overlap:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 
    bed,
    start, end,
    mean(PercentGrind) as averageGrind
from 
    BED_DATETIME as a left join
    BATTERYFEEDSAMPLE as b on b.datetime between start and end
group by bed, start, end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Mar 2021 03:50:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/722776#M224167</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-02T03:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/723047#M224313</link>
      <description>Worked well.&lt;BR /&gt;Thankyou.</description>
      <pubDate>Wed, 03 Mar 2021 00:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/723047#M224313</guid>
      <dc:creator>ZacLopresti</dc:creator>
      <dc:date>2021-03-03T00:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: Average sample data if it lays between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/723048#M224314</link>
      <description>Fantastic. Thanks</description>
      <pubDate>Wed, 03 Mar 2021 00:28:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-sample-data-if-it-lays-between-2-dates/m-p/723048#M224314</guid>
      <dc:creator>ZacLopresti</dc:creator>
      <dc:date>2021-03-03T00:28:02Z</dc:date>
    </item>
  </channel>
</rss>

