<?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: Macro to dynamically change the format of each variable to the maximum length found in that tabl in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679244#M36856</link>
    <description>&lt;P&gt;If you use the COMPRESS option, you can set the length of all the character variables to 32767, and the option will remove blanks at the end. Works like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(compress=char);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
    <pubDate>Tue, 25 Aug 2020 18:28:18 GMT</pubDate>
    <dc:creator>TomKari</dc:creator>
    <dc:date>2020-08-25T18:28:18Z</dc:date>
    <item>
      <title>Macro to dynamically change the format of each variable to the maximum length found in that table</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679195#M36844</link>
      <description>&lt;P&gt;So my code, which works up until the actual macro at the end, seems fine in theory, but I feel like SAS is not a fan of using the macro variables inside of the format options.&amp;nbsp; Below is my code, everything up until the macro works. (Pardon the casing, I write in caps, but the first part of code I found in lowercase).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data inter;&lt;BR /&gt;set have;&lt;BR /&gt;array chars _character_;&lt;BR /&gt;length __varname $32;&lt;BR /&gt;do over chars;&lt;BR /&gt;__varname=vname(chars);&lt;BR /&gt;__length=lengthn(chars);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select __varname as varname, max(__length) as max_length&lt;BR /&gt;from inter&lt;BR /&gt;group by varname&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT COUNT(DISTINCT VARNAME) INTO :VARCNT TRIMMED&lt;BR /&gt;FROM WORK.WANT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT COMPRESS(VARNAME) INTO :VRNM1 - :VRNM&amp;amp;VARCNT&lt;BR /&gt;FROM WORK.WANT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MAX_LENGTH INTO :ML1 - :MX&amp;amp;VARCNT&lt;BR /&gt;FROM WORK.WANT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO CLEAN;&lt;BR /&gt;%DO i = 1 %TO &amp;amp;VARCNT;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;ALTER TABLE HAVE&lt;BR /&gt;MODIFY &amp;amp;&amp;amp;VRNM&amp;amp;i CHAR(&amp;amp;&amp;amp;ML&amp;amp;i.) FORMAT=$&amp;amp;ML&amp;amp;i.&lt;BR /&gt;QUIT;&lt;BR /&gt;%END;&lt;BR /&gt;%MEND;&lt;BR /&gt;%CLEAN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried different variations of "&amp;amp;"s and "."s but could not find one that worked.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 16:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679195#M36844</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T16:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679201#M36845</link>
      <description>You need two periods at the end is one issue. Check what is getting resolved using the macro debugging options:&lt;BR /&gt;&lt;BR /&gt;options mprint symbolgen;&lt;BR /&gt;&lt;BR /&gt;Run the code again and post the log from your macro with the debugging options on.</description>
      <pubDate>Tue, 25 Aug 2020 16:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679201#M36845</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-25T16:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679204#M36846</link>
      <description>&lt;P&gt;When you want a value involving macro variables to have a . in the resolved value you likely need 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ORMAT=$&amp;amp;ML&amp;amp;i..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;%let v =sometext;&lt;/P&gt;
&lt;P&gt;%put&amp;nbsp; one dot: &amp;amp;v. ;&lt;/P&gt;
&lt;P&gt;%put&amp;nbsp; two dots: &amp;amp;v..;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A single dot is used to end a macro variable so that when you may a construct like &amp;amp;v.P, intending a value like "sometextP" the processor knows to stop resolving &amp;amp;v. If you use &amp;amp;vp then the processor expects a macro variable named VP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you may be doing more work than needed. You can reference all of the character variables, if that is truly what you want with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;format _character_&amp;nbsp; $w. if you use Proc datasets&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.class;
   set sashelp.class;
run;

proc datasets library=work;
   modify class;
      format _character_ $25. ;
run;
quit;&lt;/PRE&gt;
&lt;P&gt;The data step is to create a temporary version of a data set you should have available and then use proc datasets to modify the format property.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your macro would only have to determine the length that you need one time and not have to have a very inefficient loop of Proc Sql alter table stuff.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc datasets is one of the of the other procs that requires QUIT to end.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 16:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679204#M36846</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-25T16:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679212#M36847</link>
      <description>&lt;P&gt;MPRINT(CLEAN): PROC SQL;&lt;BR /&gt;WARNING: Apparent symbolic reference VRNM not resolved.&lt;BR /&gt;SYMBOLGEN: Macro variable I resolves to 1&lt;BR /&gt;NOTE: Line generated by the invoked macro "CLEAN".&lt;BR /&gt;36 &amp;amp;VRNM&amp;amp;i CHAR(&amp;amp;&amp;amp;ML&amp;amp;i) FORMAT=$&amp;amp;ML&amp;amp;i.. QUIT;&lt;BR /&gt;_&lt;BR /&gt;22&lt;BR /&gt;200&lt;BR /&gt;SYMBOLGEN: &amp;amp;&amp;amp; resolves to &amp;amp;.&lt;BR /&gt;SYMBOLGEN: Macro variable I resolves to 1&lt;BR /&gt;SYMBOLGEN: Macro variable ML1 resolves to 20&lt;BR /&gt;WARNING: Apparent symbolic reference ML not resolved.&lt;BR /&gt;SYMBOLGEN: Macro variable I resolves to 1&lt;BR /&gt;NOTE 137-205: Line generated by the invoked macro "CLEAN".&lt;BR /&gt;36 PROC SQL; ALTER TABLE HAVE MODIFY &amp;amp;VRNM&amp;amp;i CHAR(&amp;amp;&amp;amp;ML&amp;amp;i) FORMAT=$&amp;amp;ML&amp;amp;i.. QUIT;&lt;BR /&gt;_&lt;BR /&gt;2 The SAS System 08:17 Tuesday, August 25, 2020&lt;/P&gt;&lt;P&gt;22&lt;BR /&gt;ERROR 22-322: Expecting a name.&lt;/P&gt;&lt;P&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/P&gt;&lt;P&gt;ERROR 22-322: Expecting a format name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is what shows up in the log.&amp;nbsp; I do like that mprint option though!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, for&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data work.class;
   set sashelp.class;
run;

proc datasets library=work;
   modify class;
      format _character_ $25. ;
run;
quit;&lt;/PRE&gt;&lt;P&gt;How would I amend this to make it dynamically adjust to the maximum character lengths in a table?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679212#M36847</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T17:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679216#M36848</link>
      <description>&lt;P&gt;Your subject line is confusing.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Is your goal to change the variable's defined length?&amp;nbsp; Or just the format used to display it?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;You can change the formats used to display the variable without recreating the whole dataset.&lt;/P&gt;
&lt;P&gt;If you need to change the actual definition of the variables (and not just the information on how to display the values) then you need to make a new dataset.&amp;nbsp; You could re-use the same name (as long as you are ok with loosing the original version).&lt;/P&gt;
&lt;P&gt;Also make sure to either remove existing $xx formats attached to the variables, or at least replace them with new formats that match the new lengths of the variable.&amp;nbsp; If you have a variable that can hold up to 20 characters but have attached the $10. format to it then only the first 10 will be displayed.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679216#M36848</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-25T17:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679217#M36849</link>
      <description>A format doesn't change the length at all - it just changes how much of it shows. Do you want to change the length or the format as they are not the same. &lt;BR /&gt;&lt;BR /&gt;Any chance you imported some data using PROC IMPORT and it is truncating values and this is your attempt to fix it? Changing a length or format after the fact only change how the data is displayed so isn't a useful endeavor unless something went wrong somewhere else.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679217#M36849</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-25T17:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679220#M36850</link>
      <description>&lt;P&gt;Sorry for being unclear.&amp;nbsp; I often work with and am given tables that come with formats that are much longer than required.&amp;nbsp; My issue with that is when I join back to those tables with data sets that have millions and millions of rows, these overly large formats make the table way bigger than it needs to be.&amp;nbsp; I have found that doing that alter table step and modifying the CHAR() and FORMAT($.) allows the final joined table to be much smaller, which is important in the environment I work in.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679220#M36850</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T17:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679221#M36851</link>
      <description>&lt;P&gt;If you just want the display format width to match the variable's length then why not just REMOVE any existing formats?&amp;nbsp; Then there is no need to calculate the maximum length per variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets nolist lib=work;
  modify have ;
    format _character_ ;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679221#M36851</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-25T17:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679225#M36852</link>
      <description>I tried that with the table, and in the properties section, the length seems unchanged, but the format for the characters is now blank. Doesn't length impact the size? Or is it just the format?</description>
      <pubDate>Tue, 25 Aug 2020 17:40:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679225#M36852</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T17:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679226#M36853</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316411"&gt;@lawatkey&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, for&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.class;
   set sashelp.class;
run;

proc datasets library=work;
   modify class;
      format _character_ $25. ;
run;
quit;&lt;/PRE&gt;
&lt;P&gt;How would I amend this to make it dynamically adjust to the maximum character lengths in a table?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Actually I misunderstood your requirement as setting all variables to the same format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might look at this though:&lt;/P&gt;
&lt;PRE&gt;proc datasets library=work noprint;
   modify have;
   %do i=1 to &amp;amp;varcnt.;
      format &amp;amp;&amp;amp;VRNM&amp;amp;i $&amp;amp;ML&amp;amp;i..;
   %end;
run;
quit; &lt;/PRE&gt;
&lt;P&gt;assuming your &amp;amp;ml&amp;amp;i. values are correct.&lt;/P&gt;
&lt;P&gt;I think a single call to proc datasets may be faster than multiple Proc SQL alter table calls.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679226#M36853</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-25T17:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679228#M36854</link>
      <description>&lt;P&gt;Here's an option you might find useful:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	set sashelp.cars;
run;

proc sql noprint;
	select max(length) into :maxlen from dictionary.columns where libname = "WORK" and memname = "HAVE" and type="char";
	select name into :varname separated by " " from dictionary.columns where libname = "WORK" and memname = "HAVE" and type="char";
quit;

%put &amp;amp;maxlen.;
%put &amp;amp;varname.;

data want;
	length &amp;amp;varname. $ &amp;amp;maxlen.;
	set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Aug 2020 17:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679228#M36854</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2020-08-25T17:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679235#M36855</link>
      <description>This is unfortunately not a solution for me at all, because if one variable is legitimately 1000 characters long, but the rest are 10-20, this seems to group all characters the same way. But I wonder if your code can be put into a macro to evaluate each character variable separately? Using your macro made the table about 2-3 times its original size</description>
      <pubDate>Tue, 25 Aug 2020 18:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679235#M36855</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T18:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679244#M36856</link>
      <description>&lt;P&gt;If you use the COMPRESS option, you can set the length of all the character variables to 32767, and the option will remove blanks at the end. Works like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(compress=char);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 18:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679244#M36856</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2020-08-25T18:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679246#M36857</link>
      <description>So the compress option does bring the table size down, but is this compression lost if I join this table? Since I still see the formats and lengths in the properties are unchanged.</description>
      <pubDate>Tue, 25 Aug 2020 18:35:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679246#M36857</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T18:35:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679248#M36859</link>
      <description>&lt;P&gt;Correct. I use this technique when I'm pulling in some data from "somewhere", and I don't know how long the character variables are. This way, they're accommodated no matter what, and then I restructure the dataset to reduce the sizes. You can also apply the option to a library, in which case all the datasets that are created in the library will be compressed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 18:42:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679248#M36859</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2020-08-25T18:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679249#M36860</link>
      <description>Correct as in, this compress option won't "stick" when I rejoin it?&lt;BR /&gt;I have table HAVE_1 that is millions of rows long.&lt;BR /&gt;There's also HAVE_2 that is much smaller, but has the inefficient lengths and formats.&lt;BR /&gt;My end result is a&lt;BR /&gt;&lt;BR /&gt;create table WANT&lt;BR /&gt;select&lt;BR /&gt;a.var1, b.*&lt;BR /&gt;from HAVE_1 A&lt;BR /&gt;left join HAVE_2 B&lt;BR /&gt;on A.x = B.x;&lt;BR /&gt;&lt;BR /&gt;Will the compress=char on HAVE_2 make it so that WANT will have those compressed variables from HAVE_2, or is the compression on HAVE_2 meaningless if my goal is to join it right after?</description>
      <pubDate>Tue, 25 Aug 2020 18:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679249#M36860</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T18:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679250#M36861</link>
      <description>&lt;P&gt;You seem to me using the word FORMAT as if it implied the variable type.&amp;nbsp; SAS has just two variable types, floating point numbers and fixed length character strings. For character variable it is the LENGTH that determines how the variable is defined.&amp;nbsp; FORMATs in SAS are just instructions for how to convert the stored values into text.&amp;nbsp; They are totally independent from how the variable is actually defined.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are getting dataset with variables whose defined length is much larger than the longest value you can just use the COMPRESS=YES setting and SAS will remove the trailing spaces when writing the values to the disk.&amp;nbsp; You don't really need to worry too much about lengths that are longer than the values.&amp;nbsp; (Unless you are talking about huge datasets).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are worried about combining datasets that have the same variables defined with different lengths then you should get the maximum length from all of the dataset to avoid the possibility of truncation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you do also need to worry about whether the character variables have formats attached.&amp;nbsp; When combining dataset with variables with different lengths SAS will use the length from the first dataset that has the variable.&amp;nbsp; And when combining dataset with the same variable with different formats attached SAS will use the first format it sees (ignoring any datasets where the variable does not have a format attached).&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you could cause lose of data because the variable is defined shorter than the values read from the other dataset.&amp;nbsp; But having the wrong format attached could also cause values to appear to be truncated because of the format with the wrong width is attached to the variable.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 18:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679250#M36861</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-25T18:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679253#M36862</link>
      <description>&lt;P&gt;Just set the system option COMPRESS.&amp;nbsp; That will change the default compression used when you don't explicitly set it with the dataset option COMPRESS=.&amp;nbsp; You can also set a default on the LIBNAME statement.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 18:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679253#M36862</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-25T18:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679255#M36863</link>
      <description>&lt;P&gt;If the goal is to just convert all of the character variables to minimum length needed to store the values present then something like this will work.&amp;nbsp; You can use a data step view for the first step to avoid storing the tall dataset, and keep only the variables you need, do not use LENGTHN() function as zero is an invalid length for a variable.&amp;nbsp; You can then query that view to get the max length for each variable and use it to generate one macro variable you could use in a LENGTH statement to define the variables. Then make a new dataset from the old one by placing the LENGTH statement before the SET statement so that the variables' lengths are defined before the compiler sees how they are defined in the source dataset. Add a format statement to remove any formats that might have been attached to the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _length_check / view=_length_check;
  set have;
  array chars _character_;
  length __varname $32 __length 8;
  do over chars;
    __varname=vname(chars);
    __length=length(chars);
    output;
  end;
  keep __varname __length;
run;

proc sql noprint;
  select catx(' ',nliteral(__varname),cats('$',max(__length)))
    into :_lengths separated by ' '
  from _length_check
  group by __varname 
  ;
quit;

data want ;
  length &amp;amp;_lengths ;
  set have;
  format _character_ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will have trouble if you have a lot of variables, as there is a 64K byte limit to the length of a macro variable.&lt;/P&gt;
&lt;P&gt;It will also change the position of the variables in the new dataset.&amp;nbsp; All of the character variables will now appear before any numeric variable. Also the relative order of the character variables will be based on how the SQL query generated them.&amp;nbsp; That will be potentially random however in practice PROC SQL will order them alphabetically because of the group by clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the variables positions preserved you will need a more complicated process.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 19:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679255#M36863</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-25T19:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to dynamically change the format of each variable to the maximum length found in that tabl</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679275#M36864</link>
      <description>Now this worked for me perfectly! This is exactly what I was trying to do, thank you so much!! And thank you everyone who posted here, whether they were viable solutions for my needs or not, I learned a lot about macros, formatting, and compression &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 25 Aug 2020 19:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Macro-to-dynamically-change-the-format-of-each-variable-to-the/m-p/679275#M36864</guid>
      <dc:creator>lawatkey</dc:creator>
      <dc:date>2020-08-25T19:30:27Z</dc:date>
    </item>
  </channel>
</rss>

