<?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 Appending files with a varying field length in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739519#M230828</link>
    <description>&lt;P&gt;I have two separate Excel files (Data1.xlsx and Data2.xlsx) that both have fields ID and last_name.&amp;nbsp; I am exporting both of those data sets to separate permanent SAS datasets, needing the length and format of each field to be exported the same.&amp;nbsp; In Data1.xlsx, the longest last_name I have is "Gooligan" and in Data2.xlsx, the longest last_name I have is "Van Driverson." I am able to export them, but when I go to append the 2 SAS datasets, the last_name in the second dataset gets truncated to 8 characters (since Gooligan is the max of 8 characters in first dataset.)&amp;nbsp; How do I expand last_name in Data1.xlsx to 13 characters prior to exporting it to a permanent SAS dataset so that the files will append without truncation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realize I can append the files initially in Excel, but the SAS datasets need to be used later on for other reasons and need them to be the same structure.&amp;nbsp; Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 06 May 2021 15:28:33 GMT</pubDate>
    <dc:creator>Bluekeys49</dc:creator>
    <dc:date>2021-05-06T15:28:33Z</dc:date>
    <item>
      <title>Appending files with a varying field length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739519#M230828</link>
      <description>&lt;P&gt;I have two separate Excel files (Data1.xlsx and Data2.xlsx) that both have fields ID and last_name.&amp;nbsp; I am exporting both of those data sets to separate permanent SAS datasets, needing the length and format of each field to be exported the same.&amp;nbsp; In Data1.xlsx, the longest last_name I have is "Gooligan" and in Data2.xlsx, the longest last_name I have is "Van Driverson." I am able to export them, but when I go to append the 2 SAS datasets, the last_name in the second dataset gets truncated to 8 characters (since Gooligan is the max of 8 characters in first dataset.)&amp;nbsp; How do I expand last_name in Data1.xlsx to 13 characters prior to exporting it to a permanent SAS dataset so that the files will append without truncation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realize I can append the files initially in Excel, but the SAS datasets need to be used later on for other reasons and need them to be the same structure.&amp;nbsp; Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 06 May 2021 15:28:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739519#M230828</guid>
      <dc:creator>Bluekeys49</dc:creator>
      <dc:date>2021-05-06T15:28:33Z</dc:date>
    </item>
    <item>
      <title>Re: Appending files with a varying field length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739546#M230839</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294090"&gt;@Bluekeys49&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have two separate Excel files (Data1.xlsx and Data2.xlsx) that both have fields ID and last_name.&amp;nbsp; I am exporting both of those data sets to separate permanent SAS datasets, needing the length and format of each field to be exported the same.&amp;nbsp; In Data1.xlsx, the longest last_name I have is "Gooligan" and in Data2.xlsx, the longest last_name I have is "Van Driverson." I am able to export them, but when I go to append the 2 SAS datasets, the last_name in the second dataset gets truncated to 8 characters (since Gooligan is the max of 8 characters in first dataset.)&amp;nbsp; How do I expand last_name in Data1.xlsx to 13 characters prior to exporting it to a permanent SAS dataset so that the files will append without truncation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realize I can append the files initially in Excel, but the SAS datasets need to be used later on for other reasons and need them to be the same structure.&amp;nbsp; Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To get consistent lengths in the first step data sets about your only shot starting with Excel is to save the file as CSV (or other delimited text) and use a data step to READ the data into SAS with a data step.&lt;/P&gt;
&lt;P&gt;You can combine, with a limitation that all the like named variables have the same data type (not a given with most methods for getting Excel to SAS data sets) but setting a common property before using as data step code with set statements to append the data.&lt;/P&gt;
&lt;PRE&gt;Data example;
   length lastname $ 25  firstname $15 commenttext $ 200;
   set datasetone
       datasettwo
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Since the Length statement appears before the Set to read the files that becomes the length of the variables. So if datasetone has maximum length of 8 for lastname and datasettwo has maximum length of 25 the 25 is used for combining the values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 May 2021 16:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739546#M230839</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-06T16:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Appending files with a varying field length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739572#M230852</link>
      <description>&lt;P&gt;If you make an empty dataset with the desired attributes (i.e. length), then you can use it to guide the length of appending.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dummy_data;
  length last_name $30 first_name&amp;nbsp;$20;
  stop;
run;
data mydata;
  set&amp;nbsp;dummy_data data1 data2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the "data mydata" step, the length of a variable will be determined by its first reference - in this case dummy_data (with zero observations but also with metadata for each variable), which had lengths assigned in the prior step.&amp;nbsp; The advantage of this is that you can make dummy_data a permanent data set, but can subsequently use it to process any update files, (i.e. run the second step only) with no further specific length assignments.&amp;nbsp; The more variables you have to deal with, the more beneficial this technique will be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This behavior means that&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
  set data1 data2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will assign lengths determined by DATA1, as you have discovered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, you could switch the order of the data set names in the SET statement (set data2 data1).&amp;nbsp; But if you really want all the data1 cases preceding all the data2 cases, you could also&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
  if 0 then set data2;
  set data1 data2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which forces the compiler to encounter the lengths in data2 first even though the data in data2 follows data1. And of course there is a risk that data2 doesn't always have the longer lengths.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A long-winded way to suggest the dummy_data approach for any work that will be repeated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 May 2021 17:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-files-with-a-varying-field-length/m-p/739572#M230852</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-06T17:14:28Z</dc:date>
    </item>
  </channel>
</rss>

