<?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: Fixing the length of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705379#M216373</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/255120"&gt;@michokwu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you. I wouldn't say issues, just that some variables were stored in a wrong format.&lt;BR /&gt;I only used the length() function to create a variable that I could run a proc freq on. I tried to attach a pic of the output&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The number one cause of "stored in a wrong format" is using Proc format or one of the widgets that call the proc to read the data. The procedure examines only a few rows of data. If all of the values for a variable in those few rows contain only digits then almost certainly the value will be numeric even if you think it should be character such as to preserve leading zeroes in Zip codes or account numbers.&lt;/P&gt;
&lt;P&gt;Character variable lengths are set as well using only the first few rows. Depending on the file type involved that could be as few as 8. Which often leads to the values being shorter than needed/expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this sound like a possible cause?&lt;/P&gt;</description>
    <pubDate>Fri, 11 Dec 2020 19:43:56 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-12-11T19:43:56Z</dc:date>
    <item>
      <title>Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705340#M216348</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I read a csv file to SAS and one of the variables has two lengths. I tried to fix it by including a length statement but I got the same result. Is there a way to fix it? It is a character variable and the length should be 6. Thank you.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new; /*without the length statement*/
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; infile 'path/dataset.csv';
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; informat variable1 $6.;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; input variable1 $;
run;

data new; /*with the length statement*/
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; infile 'path/dataset.csv';
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; informat variable1 $6.;
        length variable1 $6.
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; input variable1 $;&lt;BR /&gt;        distlen=length(variable1);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="frequency distribution of length" style="width: 231px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52525iE6967AACE5A44404/image-dimensions/231x67?v=v2" width="231" height="67" role="button" title="length.png" alt="frequency distribution of length" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;frequency distribution of length&lt;/span&gt;&lt;/span&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 17:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705340#M216348</guid>
      <dc:creator>michokwu</dc:creator>
      <dc:date>2020-12-11T17:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705345#M216351</link>
      <description>&lt;P&gt;The LENGTH() function returns the length of the value, not how the variable is defined.&lt;/P&gt;
&lt;P&gt;SAS stores all character variables as fixed length so the LENGTH() function ignores and trailing spaces.&lt;/P&gt;
&lt;P&gt;Is there some issue you are actually having with your data?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 18:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705345#M216351</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-11T18:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705346#M216352</link>
      <description>&lt;P&gt;The length of an alphanumeric variable is the number of chars it can contain (using single-byte encoding). The function length returns the number of chars actually stored, trailing blanks aren't considered.&lt;/P&gt;
&lt;P&gt;Right now both steps you have posted create the same dataset, so the second step always overwrites the result of the first step.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 18:02:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705346#M216352</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-11T18:02:47Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705347#M216353</link>
      <description>&lt;P&gt;Why do you have the INFORMAT statement?&amp;nbsp; An informat is instructions for how to read text into values. SAS does not need any special instructions for reading text into text variables.&amp;nbsp; If you want to tell SAS how much storage it should reserve to store the variable use the LENGTH statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your second data step is missing a semi-colon.&amp;nbsp; So you are never reading any data from the CSV file. Instead you are attaching the $ informat to the variables named INPUT and VARIABLE1 ;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 18:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705347#M216353</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-11T18:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705350#M216356</link>
      <description>Thank you. I wouldn't say issues, just that some variables were stored in a wrong format.&lt;BR /&gt;I only used the length() function to create a variable that I could run a proc freq on. I tried to attach a pic of the output</description>
      <pubDate>Fri, 11 Dec 2020 18:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705350#M216356</guid>
      <dc:creator>michokwu</dc:creator>
      <dc:date>2020-12-11T18:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705353#M216357</link>
      <description>Thanks. Yes, both steps create the same dataset, the second step was an attempt to fix the issue with a variable having two lengths</description>
      <pubDate>Fri, 11 Dec 2020 18:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705353#M216357</guid>
      <dc:creator>michokwu</dc:creator>
      <dc:date>2020-12-11T18:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705356#M216359</link>
      <description>Thanks for pointing that out the missing semi-colon. It's an error from copying and pasting it here.</description>
      <pubDate>Fri, 11 Dec 2020 18:20:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705356#M216359</guid>
      <dc:creator>michokwu</dc:creator>
      <dc:date>2020-12-11T18:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: Fixing the length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705379#M216373</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/255120"&gt;@michokwu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you. I wouldn't say issues, just that some variables were stored in a wrong format.&lt;BR /&gt;I only used the length() function to create a variable that I could run a proc freq on. I tried to attach a pic of the output&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The number one cause of "stored in a wrong format" is using Proc format or one of the widgets that call the proc to read the data. The procedure examines only a few rows of data. If all of the values for a variable in those few rows contain only digits then almost certainly the value will be numeric even if you think it should be character such as to preserve leading zeroes in Zip codes or account numbers.&lt;/P&gt;
&lt;P&gt;Character variable lengths are set as well using only the first few rows. Depending on the file type involved that could be as few as 8. Which often leads to the values being shorter than needed/expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this sound like a possible cause?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Dec 2020 19:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fixing-the-length-of-a-variable/m-p/705379#M216373</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-11T19:43:56Z</dc:date>
    </item>
  </channel>
</rss>

