<?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: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254353#M56978</link>
    <description>&lt;P&gt;The real data is for duration in days and only has values &amp;gt;= 0. &amp;nbsp;The last bin is supposed to be for all durations of 53 weeks and greater, i.e. durations of 53, 54, 55, 56, ... weeks. &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Mar 2016 22:10:06 GMT</pubDate>
    <dc:creator>mduarte</dc:creator>
    <dc:date>2016-03-03T22:10:06Z</dc:date>
    <item>
      <title>Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254063#M56923</link>
      <description>&lt;P&gt;Hi - I would like a chart similar to&amp;nbsp;the following, where the x-axis is shown in weeks (note the last bin with &amp;gt;53) (but data in days). &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2145iD936B3F1121F26EE/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" width="448" height="202" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are my attempts so far:&lt;/P&gt;&lt;P&gt;1. Use SGPLOT with binwdith and xaxis&amp;nbsp;when data is not restricted. &amp;nbsp;Status:&amp;nbsp;Bins are not for 7 days. &amp;nbsp;Note that the data ranges from -377 to 277 in my example ( I need to work out how to set the seed ... ) &amp;nbsp;An explanation of this would be great ...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Use SGPLOT restric the data from 0 to 365. &amp;nbsp;Status: This gets me closer&amp;nbsp;however I don't get the last bin "&amp;gt;53". &amp;nbsp;Labels are also incorrect. Is this possible with SGPLOT without a custom format?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. &amp;nbsp;Try to create a custom format. &amp;nbsp;Status: Doesn't format the data as expected&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Test code is as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Add data */
data Hist(drop=i);
label T = "Days";
call streaminit(1);
do i = 1 to 100;
   T = rand("Normal", 120, 60); /* normal with mean 120  */
   T = round(T, 1);             /* round to nearest day */
   output;
end;
run;
proc means data=hist;
run;
Title 'Data not restricted';
/*Attempt One: Use SGPLOT with no data restriction / bins not correct */
proc sgplot data=hist;
histogram T / binwidth=7 binstart=0 showbins;
xaxis values=(0 to 365 by 7);
run;
/*Attempt Two: When data is restricted, bins are correct*/
Title "Data restricted to 0-365";
proc sgplot data=hist(where=(0&amp;lt;=T&amp;lt;=365));
histogram T / binwidth=7 binstart=0 showbins;
xaxis values=(0 to 365 by 7);
run;
/*Start of attempt Three: Use a custom format - not yet working*/
data dayToWeek;
attrib fmtname informat=$10. 
		eexcl informat=$1. 
		hlo informat=$ 1.
		label informat=$3.;
  drop i;
  retain fmtname '$dayToWeek' EEXCL 'Y' HLO '';

do i = 0 to 51;
  start = i*7;
  end = (i+1)*7;
  label = i;
  output;
  end;

  start=364;
  label = '52+';
  HLO = 'H';
	output; 
run;

proc format cntlin=dayToWeek; 
run;

proc freq data=hist;
tables T;
format T daytoweek.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 05:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254063#M56923</guid>
      <dc:creator>mduarte</dc:creator>
      <dc:date>2016-03-03T05:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254125#M56931</link>
      <description>&lt;P&gt;How about using the WEEK function to create the week number out of a date and then use the VBAR statement of SGPLOT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is a code sample.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Add data */
data Hist(drop=i);
  label T = "Days";
  call streaminit(1);

  do i = 1 to 100;
    T = rand("Normal", 120, 60); /* normal with mean 120  */
    T = round(T, 1);             /* round to nearest day */
    week = week(t, "V");
    output;
  end;
run;

proc sgplot data=hist;
  vbar week / stat=percent;
  xaxis type=linear 
    min=1 max=53
    values=( 4 to 52 by 4 )
    valueshint
  ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 10:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254125#M56931</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-03-03T10:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254130#M56933</link>
      <description>&lt;P&gt;Thanks Bruno, that is almost there (and much simpler). &amp;nbsp;Do you know how to get the last bin of &amp;gt; 53 as well? &amp;nbsp;Do you also know&amp;nbsp;my methods 1 and 3 didn't work?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 11:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254130#M56933</guid>
      <dc:creator>mduarte</dc:creator>
      <dc:date>2016-03-03T11:10:32Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254149#M56938</link>
      <description>&lt;P&gt;For method 3, the format name used starts with a $ , so this creates a character format, where as your data is numeric. Also the format would only work for positive numbers. Also the logic would only cover a specific year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not know of an easy way to label week 53, but there are only week numbers from 1 to 53. So week 53 is nothing special&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 13:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254149#M56938</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-03-03T13:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254353#M56978</link>
      <description>&lt;P&gt;The real data is for duration in days and only has values &amp;gt;= 0. &amp;nbsp;The last bin is supposed to be for all durations of 53 weeks and greater, i.e. durations of 53, 54, 55, 56, ... weeks. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 22:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254353#M56978</guid>
      <dc:creator>mduarte</dc:creator>
      <dc:date>2016-03-03T22:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254374#M56980</link>
      <description>&lt;P&gt;Once you have a variable with the week number then you can use a custom format such as :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   value weeknum
   53-high = '&amp;gt;53'
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use that format with the summary, if you presummarize data before the graph, and graph procedure associated with the week variable. The format will create the appropriate bin.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2016 22:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254374#M56980</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-03T22:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254392#M56982</link>
      <description>&lt;P&gt;I ended up using the following code ...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/*Create Format*/&lt;BR /&gt;data dayToWeek;&lt;BR /&gt;attrib fmtname informat=$10.&lt;BR /&gt; hlo informat=$1.&lt;BR /&gt; label informat=$3.;&lt;BR /&gt; drop i;&lt;BR /&gt; retain fmtname 'dayToWeek' SEXCL 'N' EEXCL 'Y' HLO '' TYPE 'N' ;&lt;BR /&gt;&lt;BR /&gt;do i = 0 to 51;&lt;BR /&gt; start = i*7;&lt;BR /&gt; end = (i+1)*7;&lt;BR /&gt; label = i;&lt;BR /&gt; output;&lt;BR /&gt; end;&lt;BR /&gt;&lt;BR /&gt; start=364;&lt;BR /&gt; label = '52+';&lt;BR /&gt; HLO = 'H';&lt;BR /&gt; EEXCL = 'N';&lt;BR /&gt; output; &lt;BR /&gt;run;&lt;BR /&gt;proc format cntlin=dayToWeek; &lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*see all formats*/&lt;BR /&gt;PROC FORMAT CNTLOUT=fmtout;&lt;BR /&gt;RUN;&lt;BR /&gt;PROC PRINT DATA=fmtout;&lt;BR /&gt;RUN; &lt;BR /&gt;&lt;BR /&gt;/*Create fake data*/&lt;BR /&gt;data Hist(drop=i);&lt;BR /&gt;label T = "Days";&lt;BR /&gt;call streaminit(1);&lt;BR /&gt;do i = 1 to 100;&lt;BR /&gt; T = rand("Normal", 220, 90); /* normal with mean 220 */&lt;BR /&gt; T = round(T, 1); /* round to nearest day */&lt;BR /&gt; output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc freq data=hist(where=(T&amp;gt;0)) NOPRINT;&lt;BR /&gt;tables T / out=work.hist_bin;&lt;BR /&gt;format T dayToWeek.;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;title 'Length of Travel';&lt;BR /&gt;proc sgplot data=work.hist_bin;&lt;BR /&gt;vbar T / response=percent;&lt;BR /&gt;xaxis label="Duration (Weeks)";&lt;BR /&gt;yaxis label="Percent";&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;NB. Found this reference extremely handy: &lt;A href="http://www2.sas.com/proceedings/forum2007/068-2007.pdf" target="_self"&gt;Creating a Format from Raw Data or a SAS® Dataset&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2016 01:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254392#M56982</guid>
      <dc:creator>mduarte</dc:creator>
      <dc:date>2016-03-04T01:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: Histogram with days formatted as weeks (and one bin per week) and help with PROC FORMAT</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254895#M57010</link>
      <description>&lt;P&gt;I was thinking again about the labeling of the x axis to 52 and &amp;gt;=53, so I found a way of doing this using the VALUESFORMAT= option on the XAXIS statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also had a closer look at your orginal graph and noticed that there is actually a bar between 52 and &amp;gt;=53&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway find below the code sample for the x axis labeling as it was in the original graph&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create Format*/
data dayToWeek;
  attrib
    fmtname informat=$10.
    hlo informat=$1.
    label informat=$3.
  ;
  drop i;
  retain
    fmtname 'dayToWeek' SEXCL 'Y' EEXCL 'N' HLO '' TYPE 'N'
  ;

  do i = 0 to 52;
    start = i*7;
    end = (i+1)*7;

    label = put(i, z2.);
    output;
  end;

  start=371;
  label = '53';
  HLO = 'H';
  EEXCL = 'N';
  output;
run;

proc format cntlin=dayToWeek;
run;

/*Create fake data*/
data Hist(drop=i);
  label T = "Days";
  call streaminit(1);

  do i = 1 to 1000;
    T = rand("Normal", 120, 90); /* normal with mean 220 */
    T = round(T, 1); /* round to nearest day */
    output;
  end;
run;

proc freq data=hist( where = ( T&amp;gt;0 )) NOPRINT;
  tables T / out=work.hist_bin;
  format T dayToWeek.;
run;

*
* convert the bin week to the real week number
*;
data hist_bin2;
  set hist_bin;
  t2 = input( vvalue(t), 8.);
run;

*
* create an all weeks data set
*;
data allWeeks;
  do t2 = 1 to 53;
    output;
  end;
run;

*
* merge data and all weeks
* convert t to char
*;
data hist_bin_all;
  merge hist_bin2 allWeeks;
  by t2;
  tc = put(t2, z2.);
run;

*
* create a format for labeling the x axis
*;
proc format;
  value $tc_display
    "04" = "4"
    "08" = "8"
    "12" = "12"
    "16" = "16"
    "20" = "20"
    "24" = "24"
    "28" = "28"
    "32" = "32"
    "36" = "36"
    "40" = "40"
    "44" = "44"
    "48" = "48"
    "52" = "52"
    "53" = "(*ESC*){unicode '2265'x}53"
    other = " "
  ;
run;

title 'Length of Travel';
ods graphics / width=1600 height=800;

proc sgplot data=work.hist_bin_all;
  vbar Tc / response=percent;
  xaxis  
    label="Duration (Weeks)"
    valuesformat=$tc_display. 
  ;
  yaxis label="Percent";
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 08:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Histogram-with-days-formatted-as-weeks-and-one-bin-per-week-and/m-p/254895#M57010</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-03-07T08:42:29Z</dc:date>
    </item>
  </channel>
</rss>

