<?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: How to conditionally change the length of any varible in given SAS dataset? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415547#M280145</link>
    <description>&lt;P&gt;Thanks again, I decided to use this code to format all char vars to its minimum len then run the procedure I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data first;
do i=1 to 10;
output;
end;
run;

data DEMO;
set first;
drop i;
length a $100 b 8. c $100;
a = '111';
b = 1111111;
c = 'aaaaaa';
run;

data tmp;
variable = 'afadfadsfa';
nvarnum = 2343242432;
run;

data demo;
merge demo tmp;
run;


proc sql noprint;
select name into: cvar1 - :cvar100000 from dictionary.columns where libname=upcase('WORK') and memname=upcase('demo') and type = 'char';
quit;

%macro help();

%do z = 1 %to &amp;amp;sqlobs.;

proc sql noprint;
select max(length(&amp;amp;&amp;amp;cvar&amp;amp;z)) into: maxlen from DEMO;
quit;

data _t_&amp;amp;&amp;amp;cvar&amp;amp;z (keep=&amp;amp;&amp;amp;cvar&amp;amp;z);
set demo (rename=(&amp;amp;&amp;amp;cvar&amp;amp;z = _&amp;amp;&amp;amp;cvar&amp;amp;z));
length &amp;amp;&amp;amp;cvar&amp;amp;z $&amp;amp;maxlen.;
&amp;amp;&amp;amp;cvar&amp;amp;z = _&amp;amp;&amp;amp;cvar&amp;amp;z;
drop _&amp;amp;&amp;amp;cvar&amp;amp;z;
run;

data demo;
set demo (drop=&amp;amp;&amp;amp;cvar&amp;amp;z);
run;

%end;

data fin;
merge _t_: demo;
run;

proc datasets noprint;
	delete _t_:;
run;

%mend help;
%help;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Nov 2017 15:45:26 GMT</pubDate>
    <dc:creator>Uknown_user</dc:creator>
    <dc:date>2017-11-22T15:45:26Z</dc:date>
    <item>
      <title>How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415473#M280135</link>
      <description>&lt;P&gt;I am trying to conditionally change the length of any varible that exceeds 50 characters. Is it possible to make this work?:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data demo;
length a $100;
a = '1111';
b = '1111111111';
run;

data reformat;
set demo;
if length _all_ &amp;gt; $50 then length = $10;
run;&lt;/PRE&gt;&lt;P&gt;Thanks for any suggestion!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 11:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415473#M280135</guid>
      <dc:creator>Uknown_user</dc:creator>
      <dc:date>2017-11-22T11:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415477#M280136</link>
      <description>&lt;P&gt;You cannot change the length of a variable inside a data step. The length of variables is set when the step is compiled, and stays fixed throughout.&lt;/P&gt;
&lt;P&gt;You need to determine the new length beforehand, and set it before it is defined by the contributing dataset.&lt;/P&gt;
&lt;P&gt;EG&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select max(length(var)) into :maxlen from have;
quit;

data want;
length var &amp;amp;maxlen.;
set have;
run;

/* or */

data want;
set have (rename=(var=_var));
length var &amp;amp;maxlen.;
var = _var;
drop _var;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Nov 2017 11:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415477#M280136</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-22T11:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415479#M280137</link>
      <description>&lt;P&gt;If you are woried from disk usage - you can add either to the configuration file or to the autoexec file&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or before any relevant step the: &lt;STRONG&gt;OPTION Compress=yes;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 11:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415479#M280137</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-11-22T11:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415497#M280138</link>
      <description>&lt;P&gt;Thanks both!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 13:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415497#M280138</guid>
      <dc:creator>Uknown_user</dc:creator>
      <dc:date>2017-11-22T13:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415504#M280139</link>
      <description>&lt;P&gt;I tried to use this solution &lt;STRONG&gt;OPTION Compress=yes;&lt;/STRONG&gt; but it did not work. My issue is that the procedure I am executing fails because some variables are kept with $1500 length thought.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I add this option right before the second data step, it still does not work and the variable is not reformated:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data demo;
length a $100;
a = '1111';
b = '1111111111';
run;

OPTION COMPRESS=yes;
data reformat;
set demo;
if length _all_ &amp;gt; $50 then length = $10;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Nov 2017 13:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415504#M280139</guid>
      <dc:creator>Uknown_user</dc:creator>
      <dc:date>2017-11-22T13:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415511#M280140</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95386"&gt;@Uknown_user&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I tried to use this solution &lt;STRONG&gt;OPTION Compress=yes;&lt;/STRONG&gt; but it did not work. My issue is that the procedure I am executing fails because some variables are kept with $1500 length thought.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I add this option right before the second data step, it still does not work and the variable is not reformated:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data demo;
length a $100;
a = '1111';
b = '1111111111';
run;

OPTION COMPRESS=yes;
data reformat;
set demo;
if length _all_ &amp;gt; $50 then length = $10;
run;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See my other post. Changing the length of a variable like this is NOT possible.&lt;/P&gt;
&lt;P&gt;length is a declarative statement that cannot be executed conditionally (as it is being acted upon while the data step is compiled).&lt;/P&gt;
&lt;P&gt;And if the length of a variable has already been set (eg because the variable is part of a dataset in the previous set statement), the length statement will have no effect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step compiler creates a variable in the PDV as soon as the variable is encountered; in the case of your code that is when the set statement is compiled.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if length _all_ &amp;gt; $50 then length = $10;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is syntactically bogus and only leads to a ERROR message. The length() function (note the brackets) can only work on ONE variable at any time. And length = $10 is an invalid assigment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maxim 2: Read the log.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 13:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415511#M280140</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-22T13:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415516#M280141</link>
      <description>&lt;P&gt;Thanks, is it possible to format all of the variables in given dataset without looping one by one?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 14:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415516#M280141</guid>
      <dc:creator>Uknown_user</dc:creator>
      <dc:date>2017-11-22T14:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415527#M280142</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95386"&gt;@Uknown_user&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, is it possible to format all of the variables in given dataset without looping one by one?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes. Draw the metadata for the columns from DICTIONARY.COLUMNS (in SQL, or SASHELP.VCOLUMN for data steps).&lt;/P&gt;
&lt;P&gt;EG&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table varnames as
select name
from dictionary.columns
where libname = 'YOURLIB' and memname = 'YOURDATA' and type = 'char' and length &amp;gt; 50;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can determine the maximum used length for each variable in a dynamically created SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call execute('proc sql noprint; select ');
do until(done);
  set varnames end=done;
  call execute('max(length(' !! trim(name) !! '))');
  if not done then call execute(',');
end;
call execute(' into ');
do until (done1);
  set varnames end=done1;
  call execute(':' !! trim(name));
  if not done1 then call execute(',');
end;
call execute('from YOURLIB.YOURDATA; quit;');
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will now have a macro variable with the name of each column, that contains the max length for that column.&lt;/P&gt;
&lt;P&gt;From that we create the final data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call execute('data want; set YOURLIB.YOURDATA (rename=(');
do until (done);
  set varnames end=done;
  call execute(trim(name) !! '=_' !! trim(name) !! ' ');
end;
call execute(')); length ');
do until (done);
  set varnames end=done;
  call execute(trim(name) !! ' $&amp;amp;' !! trim(name) !! '. ');
end;
call execute(';');
do until (done1);
  set varnames end=done1;
  call execute(trim(name) !! '=_' !! trim(name) !! ';');
end;
call execute('drop ');
do until (done2);
  set varnames end=done2;
  call execute('_' !! trim(name) !! ' ');
end;
call execute('; run;');
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that it might be necessary to use a macro quoting function to prevent premature resolution of the macro variables with the maximum lengths.&lt;/P&gt;
&lt;P&gt;Since I have nothing to test against, this code was written entirely "on the fly" and is untested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: after testing with some fake data, added stop; statements and used multiple variables for the end= condition.&lt;/P&gt;
&lt;P&gt;Should work now.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 15:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415527#M280142</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-22T15:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415533#M280143</link>
      <description>&lt;P&gt;Thanks for this bit of code.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 15:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415533#M280143</guid>
      <dc:creator>Uknown_user</dc:creator>
      <dc:date>2017-11-22T15:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415534#M280144</link>
      <description>&lt;P&gt;Note that I did some editing; macro quoting is not necessary, BTW.&lt;/P&gt;
&lt;P&gt;See this as an exercise in the usage of call execute, and some advanced programming. Feel free to ask for aspects that puzzle you.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 15:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415534#M280144</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-22T15:11:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally change the length of any varible in given SAS dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415547#M280145</link>
      <description>&lt;P&gt;Thanks again, I decided to use this code to format all char vars to its minimum len then run the procedure I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data first;
do i=1 to 10;
output;
end;
run;

data DEMO;
set first;
drop i;
length a $100 b 8. c $100;
a = '111';
b = 1111111;
c = 'aaaaaa';
run;

data tmp;
variable = 'afadfadsfa';
nvarnum = 2343242432;
run;

data demo;
merge demo tmp;
run;


proc sql noprint;
select name into: cvar1 - :cvar100000 from dictionary.columns where libname=upcase('WORK') and memname=upcase('demo') and type = 'char';
quit;

%macro help();

%do z = 1 %to &amp;amp;sqlobs.;

proc sql noprint;
select max(length(&amp;amp;&amp;amp;cvar&amp;amp;z)) into: maxlen from DEMO;
quit;

data _t_&amp;amp;&amp;amp;cvar&amp;amp;z (keep=&amp;amp;&amp;amp;cvar&amp;amp;z);
set demo (rename=(&amp;amp;&amp;amp;cvar&amp;amp;z = _&amp;amp;&amp;amp;cvar&amp;amp;z));
length &amp;amp;&amp;amp;cvar&amp;amp;z $&amp;amp;maxlen.;
&amp;amp;&amp;amp;cvar&amp;amp;z = _&amp;amp;&amp;amp;cvar&amp;amp;z;
drop _&amp;amp;&amp;amp;cvar&amp;amp;z;
run;

data demo;
set demo (drop=&amp;amp;&amp;amp;cvar&amp;amp;z);
run;

%end;

data fin;
merge _t_: demo;
run;

proc datasets noprint;
	delete _t_:;
run;

%mend help;
%help;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Nov 2017 15:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-change-the-length-of-any-varible-in-given/m-p/415547#M280145</guid>
      <dc:creator>Uknown_user</dc:creator>
      <dc:date>2017-11-22T15:45:26Z</dc:date>
    </item>
  </channel>
</rss>

