<?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: changing the length of a character variable redux in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782605#M249505</link>
    <description>You can rename the variable and change the length after the fact which affects the variable properties, but not the variable values. If you read it in truncated it's truncated, garbage in = garbage out for lack of a better analogy. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Fri, 26 Nov 2021 22:21:27 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-11-26T22:21:27Z</dc:date>
    <item>
      <title>changing the length of a character variable redux</title>
      <link>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782572#M249481</link>
      <description>&lt;P&gt;SAS Programmers:&lt;/P&gt;&lt;P&gt;I spent an inordinate amount of time troubleshooting a “simple” problem of changing the length of a character variable in SAS. Even though I found a solution, I’m still befuddled and hoping someone here can explain.&lt;/P&gt;&lt;P&gt;A data set was imported from a CSV file via Proc Import per the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%Macro ReadCSV (infile , outfile );
PROC IMPORT OUT= &amp;amp;outfile
             DATAFILE= &amp;amp;infile
              DBMS=CSV REPLACE;
               GETNAMES=YES;
              DATAROW=2;
		   	 GUESSINGROWS=200;
 run;
%Mend ReadCSV;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note that GUESSINGROWS was initially set to 75. Maximum character length of the values for the variable Company was 12, which appeared after line 75 in the table. SAS truncated the variable names to 8 characters, as reported by Proc Contents:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Guessingrows 75.png" style="width: 315px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66129i071205352700A1D1/image-size/large?v=v2&amp;amp;px=999" role="button" title="Guessingrows 75.png" alt="Guessingrows 75.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I tried changing the length in a subsequent data step:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data a;
	length Company $12;
	format Company $12.;
set a;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Proc Contents returned the following attributes:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="data step Proc contents.png" style="width: 313px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66130iEA9D99D9AFA8F13C/image-size/large?v=v2&amp;amp;px=999" role="button" title="data step Proc contents.png" alt="data step Proc contents.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;That didn’t help. When printed, variable names for Company were still truncated at 8 characters. Then I tried the following Proc SQL code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
alter table work.a
  modify Company char(12) format=$12. informat=$12.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Proc Contents returned the following attributes:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Proc SQL atttributes.png" style="width: 312px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66131i3C741C0890CB6A01/image-size/large?v=v2&amp;amp;px=999" role="button" title="Proc SQL atttributes.png" alt="Proc SQL atttributes.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Even though this shows Length, Format, and Informat attributes with 12 characters, printed variable names for Company were still truncated @ 8 characters.&lt;/P&gt;&lt;P&gt;After some hair pulling, I went back and changed the GUESSINGROW value from 75 to 200 in Proc Import and re-ran the code. Proc Contents returned the following attributes:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Attributes guessingrows 200.png" style="width: 323px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66132i35BFA909B54EB6BC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Attributes guessingrows 200.png" alt="Attributes guessingrows 200.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This changed the character value from 8 to 12, printing just as I wanted. However, except for the variable number, these attributes are identical to that obtained via Proc SQL! Even when the GUESSINGROWS statement was deleted, I couldn’t effect any change in printed character length using the above data step or Proc SQL code regardless of what Proc Contents was showing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone explain what’s going on here? Are there different syntax rules for changing the length of a character variable in a user programmed data step or in Proc SQL subsequent to Proc Import &amp;nbsp;vs. reading data via the Input statement?&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 17:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782572#M249481</guid>
      <dc:creator>Kip1</dc:creator>
      <dc:date>2021-11-26T17:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: changing the length of a character variable redux</title>
      <link>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782577#M249486</link>
      <description>&lt;P&gt;1. You read in the data with PROC IMPORT and get a truncated variable stored with a length of 8. That means only 8 characters are stored and the remainder are discarded from the data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. You try and increase the variable length to 12 to get the full variable, except it's already been truncated when it was read in.When SAS reads it in as truncated it has no idea that "New York City"&amp;nbsp; isn't just "New York C" so changing the length after the import doesn't fix the issue because it's already truncated in the data. You need to fix it in the import step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. You know how to fix it in the IMPORT step, setting GUESSINGROWS to a higher number, the default is higher than 75 is my guess.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. PROC IMPORT is a guessing procedure, if you want to specify types and get them the same each time you should write a data step to import your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/243227"&gt;@Kip1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;SAS Programmers:&lt;/P&gt;
&lt;P&gt;I spent an inordinate amount of time troubleshooting a “simple” problem of changing the length of a character variable in SAS. Even though I found a solution, I’m still befuddled and hoping someone here can explain.&lt;/P&gt;
&lt;P&gt;A data set was imported from a CSV file via Proc Import per the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%Macro ReadCSV (infile , outfile );
PROC IMPORT OUT= &amp;amp;outfile
             DATAFILE= &amp;amp;infile
              DBMS=CSV REPLACE;
               GETNAMES=YES;
              DATAROW=2;
		   	 GUESSINGROWS=200;
 run;
%Mend ReadCSV;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that GUESSINGROWS was initially set to 75. Maximum character length of the values for the variable Company was 12, which appeared after line 75 in the table. SAS truncated the variable names to 8 characters, as reported by Proc Contents:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Guessingrows 75.png" style="width: 315px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66129i071205352700A1D1/image-size/large?v=v2&amp;amp;px=999" role="button" title="Guessingrows 75.png" alt="Guessingrows 75.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I tried changing the length in a subsequent data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data a;
	length Company $12;
	format Company $12.;
set a;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Proc Contents returned the following attributes:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="data step Proc contents.png" style="width: 313px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66130iEA9D99D9AFA8F13C/image-size/large?v=v2&amp;amp;px=999" role="button" title="data step Proc contents.png" alt="data step Proc contents.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;That didn’t help. When printed, variable names for Company were still truncated at 8 characters. Then I tried the following Proc SQL code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
alter table work.a
  modify Company char(12) format=$12. informat=$12.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Proc Contents returned the following attributes:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Proc SQL atttributes.png" style="width: 312px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66131i3C741C0890CB6A01/image-size/large?v=v2&amp;amp;px=999" role="button" title="Proc SQL atttributes.png" alt="Proc SQL atttributes.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Even though this shows Length, Format, and Informat attributes with 12 characters, printed variable names for Company were still truncated @ 8 characters.&lt;/P&gt;
&lt;P&gt;After some hair pulling, I went back and changed the GUESSINGROW value from 75 to 200 in Proc Import and re-ran the code. Proc Contents returned the following attributes:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Attributes guessingrows 200.png" style="width: 323px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66132i35BFA909B54EB6BC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Attributes guessingrows 200.png" alt="Attributes guessingrows 200.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;This changed the character value from 8 to 12, printing just as I wanted. However, except for the variable number, these attributes are identical to that obtained via Proc SQL! Even when the GUESSINGROWS statement was deleted, I couldn’t effect any change in printed character length using the above data step or Proc SQL code regardless of what Proc Contents was showing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone explain what’s going on here? Are there different syntax rules for changing the length of a character variable in a user programmed data step or in Proc SQL subsequent to Proc Import &amp;nbsp;vs. reading data via the Input statement?&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 17:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782577#M249486</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-26T17:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: changing the length of a character variable redux</title>
      <link>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782580#M249489</link>
      <description>&lt;P&gt;If you are going to use PROC IMPORT to read a text file instead of reading it yourself you should at least use GUESSINGROWS=MAX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or use a better tool for guessing how to read a CSV file.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/csv2ds.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/csv2ds.sas&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 17:45:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782580#M249489</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-26T17:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: changing the length of a character variable redux</title>
      <link>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782599#M249501</link>
      <description>&lt;P&gt;So what you're saying is there's no way to change the length of a character variable after it's been truncated and stored with a length of 8. I didn't know this. I assume you could rename the variable with a length of 12 in the data step above because the column length is in fact 12 characters wide as reported by Proc Contents.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 21:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782599#M249501</guid>
      <dc:creator>Kip1</dc:creator>
      <dc:date>2021-11-26T21:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: changing the length of a character variable redux</title>
      <link>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782605#M249505</link>
      <description>You can rename the variable and change the length after the fact which affects the variable properties, but not the variable values. If you read it in truncated it's truncated, garbage in = garbage out for lack of a better analogy. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 26 Nov 2021 22:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/changing-the-length-of-a-character-variable-redux/m-p/782605#M249505</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-26T22:21:27Z</dc:date>
    </item>
  </channel>
</rss>

