<?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 average using the data part (Month) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868257#M342971</link>
    <description>&lt;P&gt;DATEPART, given a SAS datetime value will return a date value, which is the number of days since 01JAN1960 and is almost certainly not involved in any reasonable way with calculating an "average". I expect that you are getting errors because datepart expects something that resolves to a numeric value and I doubt that (date_opened,'month') qualifies.&lt;/P&gt;
&lt;P&gt;If you were wanting a month from the date then perhaps: month(datepart(date_opened)) but there is no way that dividing by a month number makes sense.&lt;/P&gt;
&lt;P&gt;GROUPBY the calendar month perhaps. But using just Month leaves you subject to dates across years being counted as one.&lt;/P&gt;
&lt;P&gt;Your code:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want as 
select sum(account) / (datepart (date_opened,'month')) as avg
/*date_opened ,AccNrSer*/
from work.have ;
quit;&lt;/PRE&gt;
&lt;P&gt;Provide some example data if you expect a working solution and the shown expected outcome is not a data set, or is very poorly structured on. A REPORT perhaps.&lt;/P&gt;</description>
    <pubDate>Wed, 05 Apr 2023 15:59:17 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-04-05T15:59:17Z</dc:date>
    <item>
      <title>calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868236#M342956</link>
      <description>&lt;P&gt;Good day expects.&lt;/P&gt;&lt;P&gt;i want to calculate the average of the open accounts in a month using the below sas script&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Expected outcome&lt;/CODE&gt;&lt;/PRE&gt;&lt;LI-SPOILER&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Rixile106_1-1680701095707.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/82358iABD0ABDF0E04A203/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Rixile106_1-1680701095707.png" alt="Rixile106_1-1680701095707.png" /&gt;&lt;/span&gt;&lt;PRE&gt;proc sql;
create table want as 
select sum(account) / (datepart (date_opened,'month')) as avg
/*date_opened ,AccNrSer*/
from work.have ;
quit;&lt;/PRE&gt;&lt;/LI-SPOILER&gt;&lt;PRE&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Apr 2023 13:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868236#M342956</guid>
      <dc:creator>Rixile106</dc:creator>
      <dc:date>2023-04-05T13:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868241#M342960</link>
      <description>&lt;P&gt;What is wrong with the code you show? How is this avarage [sic] computed?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 13:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868241#M342960</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-05T13:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868248#M342966</link>
      <description>the code is not giving me the expected results&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Apr 2023 13:59:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868248#M342966</guid>
      <dc:creator>Rixile106</dc:creator>
      <dc:date>2023-04-05T13:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868250#M342967</link>
      <description>&lt;P&gt;Please&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/399790"&gt;@Rixile106&lt;/a&gt;&amp;nbsp;notice that I already asked a question about your expected results, which has not been answered. Saying "does not give me the expected results" does not in any way indicate what you do expect. We need to know what you do expect in order to write code.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 14:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868250#M342967</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-05T14:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868257#M342971</link>
      <description>&lt;P&gt;DATEPART, given a SAS datetime value will return a date value, which is the number of days since 01JAN1960 and is almost certainly not involved in any reasonable way with calculating an "average". I expect that you are getting errors because datepart expects something that resolves to a numeric value and I doubt that (date_opened,'month') qualifies.&lt;/P&gt;
&lt;P&gt;If you were wanting a month from the date then perhaps: month(datepart(date_opened)) but there is no way that dividing by a month number makes sense.&lt;/P&gt;
&lt;P&gt;GROUPBY the calendar month perhaps. But using just Month leaves you subject to dates across years being counted as one.&lt;/P&gt;
&lt;P&gt;Your code:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table want as 
select sum(account) / (datepart (date_opened,'month')) as avg
/*date_opened ,AccNrSer*/
from work.have ;
quit;&lt;/PRE&gt;
&lt;P&gt;Provide some example data if you expect a working solution and the shown expected outcome is not a data set, or is very poorly structured on. A REPORT perhaps.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 15:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868257#M342971</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-05T15:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868262#M342975</link>
      <description>Your script is wrong. &lt;BR /&gt;&lt;BR /&gt;Datepart takes a single parameter, a date variable. MONTH() as a function calculates the month from a date. &lt;BR /&gt;&lt;BR /&gt;That SQL is very wrong, I would suggest showing your data (not in a spoiler or as an image) and what you're trying to calculate. &lt;BR /&gt;&lt;BR /&gt;Assuming your top 3 rows are correct, to calculate the sum and average, you could use SQL such as:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select sum(total) as Total, mean(total) as Average&lt;BR /&gt;from have;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;Then you would need to add it into your data set. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Apr 2023 14:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868262#M342975</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-05T14:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868268#M342980</link>
      <description>date_open is a numeric field.&lt;BR /&gt;below is an example of a data set&lt;BR /&gt;&lt;BR /&gt;account date_open&lt;BR /&gt;56611781 20220131&lt;BR /&gt;76254189 20220131&lt;BR /&gt;2504589 20220231&lt;BR /&gt;2508862 20221231&lt;BR /&gt;115234141 20211231&lt;BR /&gt;680315210014 20221001&lt;BR /&gt;680914510015 202191101&lt;BR /&gt;680914510016 202130125&lt;BR /&gt;680914510014 202130125&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 05 Apr 2023 14:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868268#M342980</guid>
      <dc:creator>Rixile106</dc:creator>
      <dc:date>2023-04-05T14:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868271#M342982</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/399790"&gt;@Rixile106&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;date_open is a numeric field.&lt;BR /&gt;below is an example of a data set&lt;BR /&gt;&lt;BR /&gt;account date_open&lt;BR /&gt;56611781 20220131&lt;BR /&gt;76254189 20220131&lt;BR /&gt;2504589 20220231&lt;BR /&gt;2508862 20221231&lt;BR /&gt;115234141 20211231&lt;BR /&gt;680315210014 20221001&lt;BR /&gt;680914510015 202191101&lt;BR /&gt;680914510016 202130125&lt;BR /&gt;680914510014 202130125&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Explaining the problem to us should not be as difficult as you are making it. Your original problem had a variable called "Accounts Open Per Month", where is that? How can we provide code if the data you just showed does not connect with the original problem statement? Whatever you tell us needs to be consistent with the original problem statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SAS, 20221001 is not a date. It is simply a big integer 20,221,001 that has no calendar related meaning. If you want to treat this as a year/month/date, you can create a valid SAS date value like this (in a DATA step)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;yearmonth=input(put(date_open,8.),yymmdd8.);
format yearmonth monyy7.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or in PROC SQL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input(put(date_open,8.),yymmdd8.) as yearmonth format=monyy7.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 15:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868271#M342982</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-05T15:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868299#M342994</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/399790"&gt;@Rixile106&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;date_open is a numeric field.&lt;BR /&gt;below is an example of a data set&lt;BR /&gt;&lt;BR /&gt;account date_open&lt;BR /&gt;56611781 20220131&lt;BR /&gt;76254189 20220131&lt;BR /&gt;2504589 20220231&lt;BR /&gt;2508862 20221231&lt;BR /&gt;115234141 20211231&lt;BR /&gt;680315210014 20221001&lt;BR /&gt;680914510015 202191101&lt;BR /&gt;680914510016 202130125&lt;BR /&gt;680914510014 202130125&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If your date_open variable is numeric what format does it have assigned? If the format is something like BEST12. or 12. then the value is a simple number and not a date. And running datepart function against it is even more useless. See this example:&lt;/P&gt;
&lt;PRE&gt;data junk;
   file print;
   date_open= 20220131;
   y= datepart(date_open);
   put y= ;
run;&lt;/PRE&gt;
&lt;P&gt;Whic shows Y is 234. Why would you divide anything by 234 to get an "average".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run Proc Contents on your data set and show us the result. &lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 16:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868299#M342994</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-05T16:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: calculate average using the data part (Month)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868302#M342996</link>
      <description>&lt;P&gt;I don't like this approach but it will get you what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account $ date_open;
cards;
56611781 20220131
76254189 20220131
2504589 20220228
2508862 20221231
115234141 20211231
680315210014 20221001
680914510015 20191101
680914510016 20230125
680914510014 20230125
;
run;

proc sql;
create table monthly_totals as
select put(input(put(date_open, 8. -l), yymmdd8.), yymmn6.)  as Open_Month, count(distinct account) as num_accounts
from have
group by calculated open_month;
quit;

proc sql;
create table summaries as
select 'Total' as Open_Month, sum(t1.num_accounts) as num_accounts
from monthly_totals t1
union 
select 'Average' as Open_Month, mean(t2.num_accounts) as num_accounts
from monthly_totals t2;
quit;

data want;
length open_month $20.;
set monthly_totals summaries;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Apr 2023 16:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-average-using-the-data-part-Month/m-p/868302#M342996</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-05T16:28:24Z</dc:date>
    </item>
  </channel>
</rss>

