<?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: Count the number of occurrences per weeks in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420384#M280615</link>
    <description>&lt;P&gt;To be on the safe side, I'd also group by year:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  year(sell_date) as year,
  week(sell_date,'v') as week,
  count(*) as number_sold
from carssold
where model = 'truck' and color = 'brown'
group by year, week
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 12 Dec 2017 08:06:57 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-12-12T08:06:57Z</dc:date>
    <item>
      <title>Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420300#M280611</link>
      <description>&lt;P&gt;From the data bellow I want to count the number of brown trucks sold per week.&lt;/P&gt;&lt;P&gt;The week should start on Monday and finish on Sunday.&lt;/P&gt;&lt;P&gt;How can I do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data CarsSold;&lt;BR /&gt;input Observation Data model$ color$;&lt;BR /&gt;cards;&lt;BR /&gt;1 1152017&amp;nbsp;truck &amp;nbsp;brown&lt;BR /&gt;2 1162017&amp;nbsp;truck &amp;nbsp;gray&lt;BR /&gt;3 1172017&amp;nbsp;car&amp;nbsp;white&lt;BR /&gt;4 1182017&amp;nbsp;car&amp;nbsp;yellow&lt;BR /&gt;5 1192017&amp;nbsp;truck &amp;nbsp;brown&lt;BR /&gt;6 11102017&amp;nbsp;truck &amp;nbsp;brown&lt;BR /&gt;7 11112017&amp;nbsp;car&amp;nbsp;black&lt;BR /&gt;8 11122017&amp;nbsp;car&amp;nbsp;black&lt;BR /&gt;9 11132017&amp;nbsp;truck &amp;nbsp;gray&lt;BR /&gt;10 11142017&amp;nbsp;truck&amp;nbsp; brown&lt;BR /&gt;11 11152017&amp;nbsp;car&amp;nbsp;white&lt;BR /&gt;12 11162017&amp;nbsp;car&amp;nbsp;white&lt;BR /&gt;13 11172017&amp;nbsp;truck &amp;nbsp;gray&lt;BR /&gt;14 11182017&amp;nbsp;truck &amp;nbsp;gray&lt;BR /&gt;15 11192017&amp;nbsp;car&amp;nbsp;yellow&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 22:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420300#M280611</guid>
      <dc:creator>Giovani</dc:creator>
      <dc:date>2017-12-11T22:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420303#M280612</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
where color='brown';

table date;
format date weeku.;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above is an idea. You'll need to adjust your dates for it to work properly since it counts weeks as from Sunday. You could either fudge your dates by decreasing&amp;nbsp;them&amp;nbsp;by a day or you could look into a custom format. I thought the formats could account for a different starting day of the week but don't recall how to do that.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 22:50:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420303#M280612</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-12-11T22:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420305#M280613</link>
      <description>&lt;P&gt;First would be to read the DATE as a SAS date value. However it isn't possible to tell in a programmatic manner whether 1152017 is supposed to be 15 Jan 2017, 11 May 2017&amp;nbsp;or 5 Nov 2017. If you have two digit days&lt;/P&gt;
&lt;P&gt;such as 11052017 then I would Guess you mean Nov 5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why dates? Because WEEK is a date related value and you want things by week. How do you want your "week" to look like in the results?&lt;/P&gt;
&lt;P&gt;This shows a data set with SAS date values.&lt;/P&gt;
&lt;PRE&gt;data CarsSold;
   informat observation best4. date anydtdte. model color $10.;
   format date mmddyy10.;
   input Observation Date model  color ;
cards;
1 11052017 truck  brown
2 11062017 truck  gray
3 11072017 car white
4 11082017 car yellow
5 11092017 truck  brown
6 11102017 truck  brown
7 11112017 car black
8 11122017 car black
9 11132017 truck  gray
10 11142017 truck  brown
11 11152017 car white
12 11162017 car white
13 11172017 truck  gray
14 11182017 truck  gray
15 11192017 car yellow
run;&lt;/PRE&gt;
&lt;P&gt;This will display the year and number of the week within the year as&amp;nbsp;yyWww (the lower case are numbers the W is upper case preceding the week number.&lt;/P&gt;
&lt;PRE&gt;proc freq data=carssold;
   where model='truck' and color='brown';
   tables date;
   format date weeku6.;
run;&lt;/PRE&gt;
&lt;P&gt;longer versions of the weeku format will show day of week so you'd need to do a&amp;nbsp;different work to see results other than a simple proc freq.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 23:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420305#M280613</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-12-11T23:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420316#M280614</link>
      <description>&lt;P&gt;May i request your output sample basing on the input sample you provided to help me test:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; CarsSold; /*copied the sample datastep Input from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; informat observation best4. date anydtdte. model color $10.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; format date mmddyy10.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; input Observation Date model&amp;nbsp; color ;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1 11052017 truck&amp;nbsp; brown&lt;/P&gt;&lt;P&gt;2 11062017 truck&amp;nbsp; gray&lt;/P&gt;&lt;P&gt;3 11072017 car white&lt;/P&gt;&lt;P&gt;4 11082017 car yellow&lt;/P&gt;&lt;P&gt;5 11092017 truck&amp;nbsp; brown&lt;/P&gt;&lt;P&gt;6 11102017 truck&amp;nbsp; brown&lt;/P&gt;&lt;P&gt;7 11112017 car black&lt;/P&gt;&lt;P&gt;8 11122017 car black&lt;/P&gt;&lt;P&gt;9 11132017 truck&amp;nbsp; gray&lt;/P&gt;&lt;P&gt;10 11142017 truck&amp;nbsp; brown&lt;/P&gt;&lt;P&gt;11 11152017 car white&lt;/P&gt;&lt;P&gt;12 11162017 car white&lt;/P&gt;&lt;P&gt;13 11172017 truck&amp;nbsp; gray&lt;/P&gt;&lt;P&gt;14 11182017 truck&amp;nbsp; gray&lt;/P&gt;&lt;P&gt;15 11192017 car yellow&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;create table want as&lt;/P&gt;&lt;P&gt;select distinct model ,color,week(date, 'v') as week_v,&amp;nbsp; count(model) as count&lt;/P&gt;&lt;P&gt;from CarsSold&lt;/P&gt;&lt;P&gt;where color='brown' and model='truck'&lt;/P&gt;&lt;P&gt;group by week_v,model ,color;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 23:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420316#M280614</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-12-11T23:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420384#M280615</link>
      <description>&lt;P&gt;To be on the safe side, I'd also group by year:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  year(sell_date) as year,
  week(sell_date,'v') as week,
  count(*) as number_sold
from carssold
where model = 'truck' and color = 'brown'
group by year, week
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Dec 2017 08:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420384#M280615</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-12T08:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420599#M280616</link>
      <description>&lt;P&gt;Thanks for your information.&lt;/P&gt;&lt;P&gt;Your code is working. How can I construct a spreadsheet with this results?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 21:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420599#M280616</guid>
      <dc:creator>Giovani</dc:creator>
      <dc:date>2017-12-12T21:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420601#M280617</link>
      <description>&lt;P&gt;It is giving error on the count statement&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 21:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420601#M280617</guid>
      <dc:creator>Giovani</dc:creator>
      <dc:date>2017-12-12T21:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420609#M280618</link>
      <description>&lt;P&gt;It is working, but it returns one line per day having multiple values at column&amp;nbsp;number_sold.&lt;/P&gt;&lt;P&gt;There is away I can have only a single line for each week?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 21:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420609#M280618</guid>
      <dc:creator>Giovani</dc:creator>
      <dc:date>2017-12-12T21:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420626#M280619</link>
      <description>&lt;P&gt;The general form for sending results of procedures to specific output formats is ODS.&lt;/P&gt;
&lt;P&gt;The generic form is&lt;/P&gt;
&lt;P&gt;ods &amp;lt;destination&amp;gt; &amp;lt;options&amp;gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;procedures that generate output&amp;gt;&lt;/P&gt;
&lt;P&gt;ods &amp;lt;destination&amp;gt; close;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The destinations are numerous and specific ones have different options available. I suspect you might want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ods excel file="c:\path\myspreadsheetname.xlsx";&lt;/P&gt;
&lt;P&gt;&amp;lt;procedure&amp;gt;&lt;/P&gt;
&lt;P&gt;ods excel close;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where you have c:\path point to the folder you want and use the name of the spreadsheet you want.&lt;/P&gt;
&lt;P&gt;If you don't have the SAS/Access to PC file formats you might need to use tagsets.Excelxp which generates Excel compatible XML files.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 22:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420626#M280619</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-12-12T22:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420679#M280620</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/170866"&gt;@Giovani&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;It is working, but it returns one line per day having multiple values at column&amp;nbsp;number_sold.&lt;/P&gt;
&lt;P&gt;There is away I can have only a single line for each week?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Can't be. See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CarsSold;
input Observation sell_date:mmddyy8.  model$ color$;
cards;
1 01152017 truck  brown
2 01162017 truck  gray
3 01172017 car white
4 01182017 car yellow
5 01192017 truck  brown
6 11102017 truck  brown
7 11112017 car black
8 11122017 car black
9 11132017 truck  gray
10 11142017 truck  brown
11 11152017 car white
12 11162017 car white
13 11172017 truck  gray
14 11182017 truck  gray
15 11192017 car yellow
;
run;

proc sql;
create table want as
select
  year(sell_date) as year,
  week(sell_date,'v') as week,
  count(*) as number_sold
from carssold
where model = 'truck' and color = 'brown'
group by year, week
;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                number_
year    week      sold

2017      2        1   
2017      3        1   
2017     45        1   
2017     46        1   
&lt;/PRE&gt;
&lt;P&gt;As you can clearly see, one line per week. Note that dates are stored as SAS date values, not as meaningless numbers.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 07:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420679#M280620</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-13T07:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of occurrences per weeks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420847#M280621</link>
      <description>&lt;P&gt;&lt;SPAN class="UserName lia-user-name lia-user-rank-Super-User"&gt;&lt;SPAN class="lia-link-navigation lia-page-link lia-link-disabled lia-user-name-link"&gt;&lt;SPAN class="login-bold"&gt;KurtBremseryou are fantastic!!!!!!!!!&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="UserName lia-user-name lia-user-rank-Super-User"&gt;&lt;SPAN class="lia-link-navigation lia-page-link lia-link-disabled lia-user-name-link"&gt;&lt;SPAN class="login-bold"&gt;Thank very much!!!!!&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 15:43:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-occurrences-per-weeks/m-p/420847#M280621</guid>
      <dc:creator>Giovani</dc:creator>
      <dc:date>2017-12-13T15:43:35Z</dc:date>
    </item>
  </channel>
</rss>

