<?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: Combining Files - setting length of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51449#M10787</link>
    <description>An alternative is to capture the lengths of all of the character variables via PROC CONTENTS, then determine the maximum length of each and write out a subroutine that you then call before your SET statement.  That way you optimize your dataset size yet don't truncate any character variables.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Let's say you have three datasets (creatively named A, B, and C).&lt;BR /&gt;
1) Run a contents, but keep just the name, type, and length, stripping off just the character variables.&lt;BR /&gt;
2) Sort each contents dataset, renaming the length field to a unique name.&lt;BR /&gt;
3) Merge the contents together, and determine the maximum length.&lt;BR /&gt;
4) Write out a subroutine (in this case, "charlen.sas")&lt;BR /&gt;
5) %include the subroutine (I love doing this.  Your data helps you out.)&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
*** STEPS 1 &amp;amp; 2 ***;&lt;BR /&gt;
proc contents data=A noprint out=Acon(keep=name type length where=(type=2)); &lt;BR /&gt;
proc sort data=Acon(keep=name length) out=Acon2(rename=(length=lengthA)); by name; run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=B noprint out=Bcon(keep=name type length where=(type=2)); &lt;BR /&gt;
proc sort data=Bcon(keep=name length) out=Bcon2(rename=(length=lengthB)); by name; run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=C noprint out=Ccon(keep=name type length where=(type=2)); &lt;BR /&gt;
proc sort data=Ccon(keep=name length) out=Ccon2(rename=(length=lengthC)); by name; run;&lt;BR /&gt;
/* repeat as necessary for your datasets */&lt;BR /&gt;
&lt;BR /&gt;
*** STEP 3 ***;&lt;BR /&gt;
filename charlen "C:\Documents and Settings\mystuff\charlen.sas";&lt;BR /&gt;
data _null_; merge Acon2 Bcon2 Ccon2; by name; &lt;BR /&gt;
maxlen=max(lengthA,lengthB,lengthC);&lt;BR /&gt;
&lt;BR /&gt;
*** STEP 4 ***;&lt;BR /&gt;
file charlen recfm=v;&lt;BR /&gt;
put "length " name "$" maxlen +(-1) ";";&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
*** STEP 5 ***;&lt;BR /&gt;
data ABC; &lt;BR /&gt;
%include charlen;&lt;BR /&gt;
set A B C;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Mike</description>
    <pubDate>Fri, 17 Dec 2010 14:22:57 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-12-17T14:22:57Z</dc:date>
    <item>
      <title>Combining Files - setting length of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51444#M10782</link>
      <description>I am concatenating several datasets together. However, some of the character variables have different lengths and SAS determines  the length of a variable based upon the first dataset in the SET statement. Therefore, data could be truncated.&lt;BR /&gt;
&lt;BR /&gt;
What I want to do is set the length of all the character variables to 200 as a default. Any ideas how I can do this? I know I can use the LENGTH statement prior to the SET statement, but I’d have to name each variable and I don’t want to do that.&lt;BR /&gt;
&lt;BR /&gt;
Thanks</description>
      <pubDate>Thu, 16 Dec 2010 20:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51444#M10782</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-12-16T20:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Files - setting length of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51445#M10783</link>
      <description>Hello Liz,&lt;BR /&gt;
&lt;BR /&gt;
You can use _CHARACTER_ in the LENGTH statement to get this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data o;&lt;BR /&gt;
  data i;&lt;BR /&gt;
  length _character_ $200;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Thu, 16 Dec 2010 20:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51445#M10783</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2010-12-16T20:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Files - setting length of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51446#M10784</link>
      <description>Hi:&lt;BR /&gt;
  LENGTH used in this fashion will give a WARNING:&lt;BR /&gt;
[pre]&lt;BR /&gt;
615  data newclass;&lt;BR /&gt;
616    set sashelp.class;&lt;BR /&gt;
617    length _character_ $200;&lt;BR /&gt;
WARNING: Length of character variable Name has already been set.&lt;BR /&gt;
         Use the LENGTH statement as the very first statement in the DATA STEP to declare the&lt;BR /&gt;
         length of a character variable.&lt;BR /&gt;
WARNING: Length of character variable Sex has already been set.&lt;BR /&gt;
         Use the LENGTH statement as the very first statement in the DATA STEP to declare the&lt;BR /&gt;
         length of a character variable.&lt;BR /&gt;
618  run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                        &lt;BR /&gt;
And, I do not believe that using _character_ in a LENGTH statement before the SET statement will work as desired...the names of the variables have to be used.&lt;BR /&gt;
   &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 16 Dec 2010 21:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51446#M10784</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-12-16T21:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Files - setting length of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51447#M10785</link>
      <description>You will first need to determine the names of the character variables.  There are several ways to do this.  The following uses an SQL step to build a macro variable which is then used in the length statement.&lt;BR /&gt;
[pre]/*proc sql ;*/&lt;BR /&gt;
/*describe table dictionary.columns;*/&lt;BR /&gt;
/*select libname, memname, name, type*/&lt;BR /&gt;
/*      from dictionary.columns*/&lt;BR /&gt;
/*         where (libname='SASHELP' and memname='CLASS'); */&lt;BR /&gt;
/*quit;*/&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select name&lt;BR /&gt;
   into :charlist separated by ' '&lt;BR /&gt;
      from dictionary.columns&lt;BR /&gt;
         where (libname='SASHELP' and memname='CLASS' and type='char');&lt;BR /&gt;
quit;&lt;BR /&gt;
data long;&lt;BR /&gt;
   length &amp;amp;charlist $200;&lt;BR /&gt;
   set sashelp.class;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc contents data=long;&lt;BR /&gt;
   run;[/pre]</description>
      <pubDate>Thu, 16 Dec 2010 21:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51447#M10785</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-16T21:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Files - setting length of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51448#M10786</link>
      <description>Hello Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
"And, I do not believe that using _character_ in a LENGTH statement before the SET statement will work as desired...the names of the variables have to be used."&lt;BR /&gt;
&lt;BR /&gt;
You are 100% right: it does not work and indicates some issue in SAS.&lt;BR /&gt;
&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Thu, 16 Dec 2010 21:41:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51448#M10786</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2010-12-16T21:41:39Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Files - setting length of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51449#M10787</link>
      <description>An alternative is to capture the lengths of all of the character variables via PROC CONTENTS, then determine the maximum length of each and write out a subroutine that you then call before your SET statement.  That way you optimize your dataset size yet don't truncate any character variables.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Let's say you have three datasets (creatively named A, B, and C).&lt;BR /&gt;
1) Run a contents, but keep just the name, type, and length, stripping off just the character variables.&lt;BR /&gt;
2) Sort each contents dataset, renaming the length field to a unique name.&lt;BR /&gt;
3) Merge the contents together, and determine the maximum length.&lt;BR /&gt;
4) Write out a subroutine (in this case, "charlen.sas")&lt;BR /&gt;
5) %include the subroutine (I love doing this.  Your data helps you out.)&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
*** STEPS 1 &amp;amp; 2 ***;&lt;BR /&gt;
proc contents data=A noprint out=Acon(keep=name type length where=(type=2)); &lt;BR /&gt;
proc sort data=Acon(keep=name length) out=Acon2(rename=(length=lengthA)); by name; run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=B noprint out=Bcon(keep=name type length where=(type=2)); &lt;BR /&gt;
proc sort data=Bcon(keep=name length) out=Bcon2(rename=(length=lengthB)); by name; run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=C noprint out=Ccon(keep=name type length where=(type=2)); &lt;BR /&gt;
proc sort data=Ccon(keep=name length) out=Ccon2(rename=(length=lengthC)); by name; run;&lt;BR /&gt;
/* repeat as necessary for your datasets */&lt;BR /&gt;
&lt;BR /&gt;
*** STEP 3 ***;&lt;BR /&gt;
filename charlen "C:\Documents and Settings\mystuff\charlen.sas";&lt;BR /&gt;
data _null_; merge Acon2 Bcon2 Ccon2; by name; &lt;BR /&gt;
maxlen=max(lengthA,lengthB,lengthC);&lt;BR /&gt;
&lt;BR /&gt;
*** STEP 4 ***;&lt;BR /&gt;
file charlen recfm=v;&lt;BR /&gt;
put "length " name "$" maxlen +(-1) ";";&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
*** STEP 5 ***;&lt;BR /&gt;
data ABC; &lt;BR /&gt;
%include charlen;&lt;BR /&gt;
set A B C;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Mike</description>
      <pubDate>Fri, 17 Dec 2010 14:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Files-setting-length-of-variables/m-p/51449#M10787</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-12-17T14:22:57Z</dc:date>
    </item>
  </channel>
</rss>

