<?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 ODS Excel - make column width 'behave' like excel format / alignment / 'shrink to fit' in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648449#M194247</link>
    <description>&lt;P class="lia-align-left"&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;Hello,&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-align-left"&gt;(using base SAS&amp;nbsp;9.4)&lt;/P&gt;&lt;P class="lia-align-left"&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Utilizing&amp;nbsp;ODS Excel and/or PROC REPORT, want to&amp;nbsp;make&amp;nbsp;column width 'behave' like excel format / alignment / 'shrink to fit.' &lt;/FONT&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Is this even possible? &lt;/FONT&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;(see attached .xlsx)&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;dm 'log;clear';
/* DELETE FILE */
%let myfile=C:\ods_test.xlsx;
filename myfile "&amp;amp;myfile";
data _null_;
rc=fdelete("myfile");
run;

/* RAW DATA */
data raw;
var4='1234';        /*length=4  #of chars in header=4 */
var8='12345678';    /*length=8  #of chars in header=4 */ 
header_8='123';     /*length=3  #of chars in header=8 */
run;

ods noresults;
ods html close;

/* ODS EXCEL */
ods excel file="&amp;amp;myfile." 
style=styles.htmlblue options(
ABSOLUTE_COLUMN_WIDTH = "4,4,4" 
AUTOFILTER ="ALL"
FROZEN_HEADERS="ON"
SHEET_NAME='Data'
);
run;

/* PROC REPORT */
proc report data=raw  nowd  ;
column var4 var8 header_8; 
define var8 / style(column)=[width=75];        /* ?? 'shrink to fit' so that variable value of length 8 fits column width of 4 */
define header_8 / style(header)=[width=5000];  /* ?? 'shrink to fit' so that variable 'header' of length 8 chars fits column width of 4 */
run;

ods excel close;
ods html;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;If not possible to 'shrink to fit', is it&amp;nbsp;possible&amp;nbsp;to find the 'longer of':&lt;/P&gt;&lt;P class="lia-align-left"&gt;i)&amp;nbsp; &amp;nbsp;the given length of the SAS variable OR&lt;/P&gt;&lt;P class="lia-align-left"&gt;ii)&amp;nbsp; the width&amp;nbsp;(# of chars) of the 'column header'&amp;nbsp;&amp;nbsp; ..&lt;/P&gt;&lt;P class="lia-align-left"&gt;.. utilizing this code snippet as a base?&lt;/P&gt;&lt;P class="lia-align-left"&gt;(this might conceivable&amp;nbsp;allow all 'cell content' and/or&amp;nbsp;'column headers' to be visible in all circumstances).&lt;/P&gt;&lt;PRE&gt;proc sql noprint;
  select length into : length_var separated by ',' 
  from dictionary.columns
  where libname='WORK' and memname='RAW'
  order by varnum;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;For my current business purpose, wrapping&amp;nbsp;the text is not ideal but if anyone has observations about&amp;nbsp;that as well, I'll take&amp;nbsp;it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;many thanks for any insight&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 17 May 2020 22:22:15 GMT</pubDate>
    <dc:creator>Rampsas1</dc:creator>
    <dc:date>2020-05-17T22:22:15Z</dc:date>
    <item>
      <title>ODS Excel - make column width 'behave' like excel format / alignment / 'shrink to fit'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648449#M194247</link>
      <description>&lt;P class="lia-align-left"&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;Hello,&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-align-left"&gt;(using base SAS&amp;nbsp;9.4)&lt;/P&gt;&lt;P class="lia-align-left"&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Utilizing&amp;nbsp;ODS Excel and/or PROC REPORT, want to&amp;nbsp;make&amp;nbsp;column width 'behave' like excel format / alignment / 'shrink to fit.' &lt;/FONT&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Is this even possible? &lt;/FONT&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;(see attached .xlsx)&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;dm 'log;clear';
/* DELETE FILE */
%let myfile=C:\ods_test.xlsx;
filename myfile "&amp;amp;myfile";
data _null_;
rc=fdelete("myfile");
run;

/* RAW DATA */
data raw;
var4='1234';        /*length=4  #of chars in header=4 */
var8='12345678';    /*length=8  #of chars in header=4 */ 
header_8='123';     /*length=3  #of chars in header=8 */
run;

ods noresults;
ods html close;

/* ODS EXCEL */
ods excel file="&amp;amp;myfile." 
style=styles.htmlblue options(
ABSOLUTE_COLUMN_WIDTH = "4,4,4" 
AUTOFILTER ="ALL"
FROZEN_HEADERS="ON"
SHEET_NAME='Data'
);
run;

/* PROC REPORT */
proc report data=raw  nowd  ;
column var4 var8 header_8; 
define var8 / style(column)=[width=75];        /* ?? 'shrink to fit' so that variable value of length 8 fits column width of 4 */
define header_8 / style(header)=[width=5000];  /* ?? 'shrink to fit' so that variable 'header' of length 8 chars fits column width of 4 */
run;

ods excel close;
ods html;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;If not possible to 'shrink to fit', is it&amp;nbsp;possible&amp;nbsp;to find the 'longer of':&lt;/P&gt;&lt;P class="lia-align-left"&gt;i)&amp;nbsp; &amp;nbsp;the given length of the SAS variable OR&lt;/P&gt;&lt;P class="lia-align-left"&gt;ii)&amp;nbsp; the width&amp;nbsp;(# of chars) of the 'column header'&amp;nbsp;&amp;nbsp; ..&lt;/P&gt;&lt;P class="lia-align-left"&gt;.. utilizing this code snippet as a base?&lt;/P&gt;&lt;P class="lia-align-left"&gt;(this might conceivable&amp;nbsp;allow all 'cell content' and/or&amp;nbsp;'column headers' to be visible in all circumstances).&lt;/P&gt;&lt;PRE&gt;proc sql noprint;
  select length into : length_var separated by ',' 
  from dictionary.columns
  where libname='WORK' and memname='RAW'
  order by varnum;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;For my current business purpose, wrapping&amp;nbsp;the text is not ideal but if anyone has observations about&amp;nbsp;that as well, I'll take&amp;nbsp;it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;many thanks for any insight&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 May 2020 22:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648449#M194247</guid>
      <dc:creator>Rampsas1</dc:creator>
      <dc:date>2020-05-17T22:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: ODS Excel - make column width 'behave' like excel format / alignment / 'shrink to fit'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648465#M194261</link>
      <description>&lt;P&gt;1. HTML files normally "expands to fit" if columns are too narrow for the contents, and there is room in the page width. You may want to try that.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&lt;EM&gt;is it&amp;nbsp;possible&amp;nbsp;to find the 'longer of':&lt;/EM&gt;&lt;/P&gt;
&lt;P class="lia-align-left"&gt;&lt;EM&gt;i)&amp;nbsp; &amp;nbsp;the given length of the SAS variable OR&lt;/EM&gt;&lt;/P&gt;
&lt;P class="lia-align-left"&gt;&lt;EM&gt;ii)&amp;nbsp; the width&amp;nbsp;(# of chars) of the 'column header'&amp;nbsp;&amp;nbsp; ..&lt;/EM&gt;&lt;/P&gt;
&lt;P class="lia-align-left"&gt;Yes you can:&amp;nbsp;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;max(length(VAR))&lt;/FONT&gt; will give you the figure you are seeking for i), while your code should give you ii) .&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 02:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648465#M194261</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-18T02:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: ODS Excel - make column width 'behave' like excel format / alignment / 'shrink to fit'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648654#M194353</link>
      <description>&lt;P&gt;Thank you for the reply ChrisNZ.&lt;/P&gt;&lt;P&gt;Your response (of course "&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;max(length(VAR))" .. dah) &lt;/SPAN&gt;did help to trigger a solution for using the larger of the variable name length (the excel column header) or the variable value length (the excel 'cell content').&amp;nbsp;&lt;/P&gt;&lt;P&gt;This does allow - dynamically - the excel output to show the full column header when the variable length is smaller than it OR reduce the column width when the 'cell content' is shorter than the header..... so a reasonable solution (code below).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, the ideal solution is still to somehow &lt;FONT&gt;make the excel column width 'behave' the same as when you manually 'right click on entire column' / format / alignment / 'shrink to fit.'&amp;nbsp;&lt;/FONT&gt;&lt;FONT&gt;Does anyone know if this is doable ?? (result needs to be output to excel)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;There are times when a string may be too long to reasonably display inside a cell and so it would be nice to 'shrink it to fit' a defined column width in lieu of having the column width expand to accommodate the long string. (using something like "define var12 / style(column)=[cellwidth=50]" in proc report just wraps the text within the cell and thus expands the cell height ...a result that is not workable.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;many thanks again for any insight or solutions&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* RAW DATA */
data raw;
infile datalines;
input var8 : $8. var3 : $3. var12 : $12. name12_len_3 : $3. var6 : $6.; /* ABCDEFGHIJKL */
datalines ;
123456 123 12345678 123 123456
12345 1 1234567890 123 1
1234567 1 123456 12 2
;
run;

/* i)  the variable name length (what will be the excel column header) */
proc sql;
create table var_names as 
  select varnum, name 
  from dictionary.columns
  where libname='WORK' and memname='RAW'
  order by varnum;
quit ;
data name_lengths;
set var_names;
name_length=length(name);
run;

/* ii) the variable value length */
proc sql;
create table var_lengths as 
  select varnum, name, length as value_length
  from dictionary.columns
  where libname='WORK' and memname='RAW'
  order by varnum;
quit ;

/* result: the greater of i) or ii) */
data ds_lengths;
merge name_lengths var_lengths;
by varnum;
if name_length gt value_length then max_length=name_length;
else max_length=value_length;
drop name_length value_length;
run; 

/* place into macro variable */
proc sql noprint;
  select max_length into : length_mv separated by ','
  from ds_lengths; 
quit;
data _null_;
%put &amp;amp;=length_mv.;
run; 

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 18:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648654#M194353</guid>
      <dc:creator>Rampsas1</dc:creator>
      <dc:date>2020-05-18T18:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: ODS Excel - make column width 'behave' like excel format / alignment / 'shrink to fit'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648753#M194399</link>
      <description>&lt;P&gt;&amp;gt;&amp;nbsp;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;EM&gt;the ideal solution is still to somehow&amp;nbsp;make the excel column width 'behave' the same as when you manually 'right click on entire column' / format / alignment / 'shrink to fit.'&amp;nbsp;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Each reporting format has its own quirks.&lt;/P&gt;
&lt;P&gt;Excel does not autoexpand like HTML does, and other formats don't mimic Excel either.&lt;/P&gt;
&lt;P&gt;Here are some options you can use to format proc report (for example)&amp;nbsp; and that are specific to Excel outputs:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/rnd/base/ods/odsmarkup/excelxp_help.html" target="_blank"&gt;https://support.sas.com/rnd/base/ods/odsmarkup/excelxp_help.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Another way might be to predefine an Excel range with all the desired formatting and just load some data.&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 04:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/648753#M194399</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-19T04:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: ODS Excel - make column width 'behave' like excel format / alignment / 'shrink to fit'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/649549#M194751</link>
      <description>Many thanks again for the replies and insight ChrisNZ. It is appreciated.&lt;BR /&gt;I marked this as 'solved' accepting the idea that we can't necessarily have SAS make the ODS Excel mimic the exact 'shrink to fit' behavior but we can utilize format and absolute_column_width to acquire a reasonable approximation.</description>
      <pubDate>Thu, 21 May 2020 12:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ODS-Excel-make-column-width-behave-like-excel-format-alignment/m-p/649549#M194751</guid>
      <dc:creator>Rampsas1</dc:creator>
      <dc:date>2020-05-21T12:35:34Z</dc:date>
    </item>
  </channel>
</rss>

