<?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 mean based on given time period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112718#M23286</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro RateVars(sm, sy, em,ey);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %local sdate edate d m y vars;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let sdate = %sysfunc(mdy(&amp;amp;sm,1,&amp;amp;sy));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let edate = %sysfunc(mdy(&amp;amp;em,1,&amp;amp;ey));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let d = &amp;amp;sdate;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let vars = ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %do %while (&amp;amp;d le &amp;amp;edate);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let m = %sysfunc(putn(%sysfunc(month(&amp;amp;d)),z2.));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let y = %sysfunc(putn(%sysfunc(year(&amp;amp;d)),f4.0));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let vars = &amp;amp;vars Rate_&amp;amp;m._&amp;amp;y;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let d = %sysfunc(intnx(Month,&amp;amp;d,1));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;amp;vars&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;Use in data step as:&lt;/P&gt;&lt;P&gt;data new;&lt;BR /&gt; set have;&lt;BR /&gt; average = mean(of %RateVars (&amp;amp;StartDateMonth,&amp;amp;StartDateYear,&amp;amp;EndDateMonth,&amp;amp;EndDateYear))&amp;nbsp; ;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 02 Jan 2013 21:14:19 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2013-01-02T21:14:19Z</dc:date>
    <item>
      <title>Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112713#M23281</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This seems to be a fairly easy problem to solve, but for some reason I cannot think of a way to solve it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have four macro variables, StartDateMonth, StartDateYear, EndDateMonth and EndDateYear that the user enters in. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The variable names in my data set are in the form of Rate_01_2010, Rate_02_2010, Rate_03_2010, Rate_4_2010, ...., Rate_12_2012. I'd like to calculate the average of the rates based on the dates that the user enters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%Let StartDateMonth = 6;&lt;/P&gt;&lt;P&gt;%Let StartDateYear = 2010;&lt;/P&gt;&lt;P&gt;%Let EndDateMonth = 3;&lt;/P&gt;&lt;P&gt;%Let EndDateYear = 2012;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Basically, I would want the calculation&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Average = mean(Rate_06_2010, Rate_07_2010, ....., Rate_02_2012, Rate_03_2012);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I'd like this to be done automatically, based on the inputs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for the help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 20:06:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112713#M23281</guid>
      <dc:creator>stat11</dc:creator>
      <dc:date>2013-01-02T20:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112714#M23282</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;try:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Average = mean(Rate_&amp;amp;StartDateMonth._&amp;amp;StartDateYear, Rate_%eval(&amp;amp;StartDateMonth+1.)_&amp;amp;StartDateYear,&lt;/P&gt;&lt;P&gt;Rate_%eval(&amp;amp;EndDateMonth.-1)._&amp;amp;EndDateYear, Rate_&amp;amp;EndDateMonth._&amp;amp;EndDateYear);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;it will not work at the beginning and the end of a year.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 20:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112714#M23282</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2013-01-02T20:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112715#M23283</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;One way could be to rearrange your data from wide-to-long, thus having one rate column and a date column.&lt;/P&gt;&lt;P&gt;Then you can use either proc sql or proc summary/means to summarize the rate (column) using a where statement that uses your macro variables, something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data long;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set your_wide_data;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rate = rate_03_2010;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date = mdy(03,01,2010);*I chose the day to be the first (01) since it doesn't seem to matter for your problem;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rate = rate_04_2010;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date = mdy(06,01,2010);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rate = rate_06_2012;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date = mdy(06,01,2012);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc means data = long;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var rate;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where mdy(&amp;amp;StartDateMonth., 01, &amp;amp;StartDateYear. ) &amp;lt;= date &amp;lt;= mdy(&amp;amp;EndDateMonth.,01, &amp;amp;endDateYear.);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table averages_&amp;amp;StartDateMonth as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select avg(rate) as average_rate&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from long&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where mdy(&amp;amp;StartDateMonth., 01, &amp;amp;StartDateYear. ) &amp;lt;= date &amp;lt;= mdy(&amp;amp;EndDateMonth.,01, &amp;amp;endDateYear.);&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best of luck,&lt;/P&gt;&lt;P&gt;Anca.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 20:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112715#M23283</guid>
      <dc:creator>AncaTilea</dc:creator>
      <dc:date>2013-01-02T20:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112716#M23284</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You may try below code and see if it works for you. Good Luck.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; length varlist $1000;&lt;/P&gt;&lt;P&gt;&amp;nbsp; retain varlist;&lt;/P&gt;&lt;P&gt;&amp;nbsp; sdate = input(compress("&amp;amp;StartDateYear"||'-'||put(input("&amp;amp;StartDateMonth",best.),z2.)||'-01'),yymmdd10.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; edate = input(compress("&amp;amp;EndDateYear"||'-'||put(input("&amp;amp;EndDateMonth",best.),z2.)||'-01'),yymmdd10.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; do dates = sdate to edate;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if day(dates)=1 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; month = put(month(dates),z2.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; year = put(year(dates),4.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; if varlist='' then varlist = 'Rate_'||strip(month)||'_'||strip(year);&lt;/P&gt;&lt;P&gt;&amp;nbsp; else varlist = strip(varlist)||', '||'Rate_'||strip(month)||'_'||strip(year);&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; call symput('varlist',varlist);&lt;/P&gt;&lt;P&gt;&amp;nbsp; format sdate edate dates date9.;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put &amp;amp;varlist;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Average = mean(&amp;amp;varlist);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 20:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112716#M23284</guid>
      <dc:creator>NickR</dc:creator>
      <dc:date>2013-01-02T20:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112717#M23285</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you're lucky, this will be easy.&amp;nbsp; "Lucky" here means that the rate variables are internally stored in date order with no other variables in between.&amp;nbsp; PROC CONTENTS will show you that.&amp;nbsp; In that case, you could just use:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;average = mean(of rate_&amp;amp;StartDateMonth._&amp;amp;StartDateYear -- rate_&amp;amp;EndDateMonth._&amp;amp;EndDateYear);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the variables are in order, but they are interspersed with character variables, it is still possible:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;average = mean(of rate_&amp;amp;StartDateMonth._&amp;amp;StartDateYear -numeric- rate_&amp;amp;EndDateMonth._&amp;amp;EndDateYear);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 21:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112717#M23285</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-01-02T21:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112718#M23286</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro RateVars(sm, sy, em,ey);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %local sdate edate d m y vars;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let sdate = %sysfunc(mdy(&amp;amp;sm,1,&amp;amp;sy));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let edate = %sysfunc(mdy(&amp;amp;em,1,&amp;amp;ey));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let d = &amp;amp;sdate;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let vars = ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %do %while (&amp;amp;d le &amp;amp;edate);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let m = %sysfunc(putn(%sysfunc(month(&amp;amp;d)),z2.));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let y = %sysfunc(putn(%sysfunc(year(&amp;amp;d)),f4.0));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let vars = &amp;amp;vars Rate_&amp;amp;m._&amp;amp;y;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let d = %sysfunc(intnx(Month,&amp;amp;d,1));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;amp;vars&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;Use in data step as:&lt;/P&gt;&lt;P&gt;data new;&lt;BR /&gt; set have;&lt;BR /&gt; average = mean(of %RateVars (&amp;amp;StartDateMonth,&amp;amp;StartDateYear,&amp;amp;EndDateMonth,&amp;amp;EndDateYear))&amp;nbsp; ;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 21:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112718#M23286</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2013-01-02T21:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate mean based on given time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112719#M23287</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you! That works perfectly!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Jan 2013 21:56:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-mean-based-on-given-time-period/m-p/112719#M23287</guid>
      <dc:creator>stat11</dc:creator>
      <dc:date>2013-01-02T21:56:12Z</dc:date>
    </item>
  </channel>
</rss>

