<?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 PROC FREQ order for day of the week (downame.) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931027#M366284</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose I have a date variable, and I want to use PROC FREQ to make a frequency tables of day of the week (Monday-Sunday), and I want the frequency table to be ordered reasonably (Mon-Sun, or Sun-Sat, or whatever).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But my data is sparse, so might look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input dt date9. ;
  cards ;
03JUN2024
07JUN2024
11JUN2024
13JUN2024
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have ;
  tables dt ;
  format dt downame. ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the default order=internal, which means the day order looks weird:&lt;/P&gt;
&lt;PRE&gt;        dt    Frequency
 ----------------------
    Monday           1
    Friday           1
   Tuesday           1
  Thursday           1
&lt;/PRE&gt;
&lt;P&gt;Order=formatted would be alphabetical, no better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best I could do was sort day by day of the week (1-7), and then use order=data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as 
  select * from have
  order by put(dt,weekday.)
  ;
quit ;

proc freq data=want order=data;
  tables dt ;
  format dt downame. ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which works, but seems like a lot of hoops.&lt;/P&gt;
&lt;PRE&gt;        dt    Frequency
 ----------------------
    Monday           1
   Tuesday           1
  Thursday           1
    Friday           1
&lt;/PRE&gt;
&lt;P&gt;Am I missing something obvious?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I know it could be done with PROC TABULATE and preloadformat, but I want to stick with PROC FREQ to get some statistics out of it.&lt;/P&gt;</description>
    <pubDate>Wed, 05 Jun 2024 19:13:35 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2024-06-05T19:13:35Z</dc:date>
    <item>
      <title>PROC FREQ order for day of the week (downame.)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931027#M366284</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose I have a date variable, and I want to use PROC FREQ to make a frequency tables of day of the week (Monday-Sunday), and I want the frequency table to be ordered reasonably (Mon-Sun, or Sun-Sat, or whatever).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But my data is sparse, so might look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input dt date9. ;
  cards ;
03JUN2024
07JUN2024
11JUN2024
13JUN2024
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have ;
  tables dt ;
  format dt downame. ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the default order=internal, which means the day order looks weird:&lt;/P&gt;
&lt;PRE&gt;        dt    Frequency
 ----------------------
    Monday           1
    Friday           1
   Tuesday           1
  Thursday           1
&lt;/PRE&gt;
&lt;P&gt;Order=formatted would be alphabetical, no better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best I could do was sort day by day of the week (1-7), and then use order=data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as 
  select * from have
  order by put(dt,weekday.)
  ;
quit ;

proc freq data=want order=data;
  tables dt ;
  format dt downame. ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which works, but seems like a lot of hoops.&lt;/P&gt;
&lt;PRE&gt;        dt    Frequency
 ----------------------
    Monday           1
   Tuesday           1
  Thursday           1
    Friday           1
&lt;/PRE&gt;
&lt;P&gt;Am I missing something obvious?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I know it could be done with PROC TABULATE and preloadformat, but I want to stick with PROC FREQ to get some statistics out of it.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2024 19:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931027#M366284</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-06-05T19:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: PROC FREQ order for day of the week (downame.)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931034#M366285</link>
      <description>&lt;P&gt;Not super useful I'm afraid&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input dt date9. ;
  dt2=dt;
  cards ;
03JUN2024
07JUN2024
11JUN2024
13JUN2024
;
run ;


proc freq data=have order=formatted;
  tables dt2*dt / list;
  format dt2 weekday. dt downame. ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jun 2024 19:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931034#M366285</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-06-05T19:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: PROC FREQ order for day of the week (downame.)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931035#M366286</link>
      <description>&lt;P&gt;Proc Tabulate and PRELOADFMT will not solve this particular problem, at least without a bit of work. You will get&lt;/P&gt;
&lt;PRE&gt;WARNING: The format for variable dt cannot be preloaded. A finite set of formatted values cannot
         be produced from the format.
&lt;/PRE&gt;
&lt;P&gt;Use the Weekday function on the date to create a new variable and do the analysis on that. And a custom format to map the number back to days of the week.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2024 20:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931035#M366286</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-05T20:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: PROC FREQ order for day of the week (downame.)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931038#M366288</link>
      <description>&lt;P&gt;Thanks, in real problem all I want is a table of DayOfTheWeek*Success(1/0) to look if failures differ by day of the week.&amp;nbsp; So I can't add a variable to the table statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess another option would be to make a variable with values 1-7, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input dt date9. ;
  day=weekday(dt); *returns 1-7 for Sun-Sat ;
  cards ;
03JUN2024
07JUN2024
11JUN2024
13JUN2024
;
run ;


*is there really no built-in format for this? ;
proc format ;
 value dow
   1="Sun"
   2="Mon"
   3="Tue"
   4="Wed"
   5="Thur"
   6="Fri"
   7="Sat"
 ;
run ;

proc freq data=have order=internal;
  tables day ;
  format day dow. ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2024 20:09:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931038#M366288</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-06-05T20:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: PROC FREQ order for day of the week (downame.)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931040#M366289</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;.&amp;nbsp; Saw this after I wrote an example of that approach.&amp;nbsp; : )&lt;BR /&gt;&lt;BR /&gt;Shocked there isn't a format out of the box to map 1=Sun ... 7=Sat.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2024 20:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931040#M366289</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-06-05T20:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: PROC FREQ order for day of the week (downame.)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931052#M366292</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;.&amp;nbsp; Saw this after I wrote an example of that approach.&amp;nbsp; : )&lt;BR /&gt;&lt;BR /&gt;Shocked there isn't a format out of the box to map 1=Sun ... 7=Sat.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just like there is no 1=January 2=February etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe time to ask about an order= for date values other than data/internal/formatted/freq...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually if you only have ONE of these sparse date values then WEEKDAY function +sort and Order=data works.&lt;/P&gt;
&lt;PRE&gt;data have ;
  input dt date9. ;
  wd = weekday(dt);
  cards ;
03JUN2024
07JUN2024
11JUN2024
13JUN2024
;
run ;

proc sort data=have;
   by wd;
run;

proc freq data=have order=data;
   tables dt;
   format dt downame9.;
run;&lt;/PRE&gt;
&lt;P&gt;But if you have multiple variables you have to sort the data by each corresponding day of week created variable (or recreate and re-sort). So if there are multiple variables involved it may be worth the custom day of week format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2024 21:05:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-FREQ-order-for-day-of-the-week-downame/m-p/931052#M366292</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-05T21:05:17Z</dc:date>
    </item>
  </channel>
</rss>

