<?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: Importing a char variable of different lengths in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567558#M159618</link>
    <description>&lt;P&gt;See this code as an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile datalines dlm="," dsd;
input number string:$char12.;
test_of_string = put(string,$hex24.);
datalines4;
1,ABC
2,  DEF
3,XYZ
;;;;

proc print data=test noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;number    string         test_of_string

   1      ABC       414243202020202020202020
   2        DEF     202044454620202020202020
   3      XYZ       58595A202020202020202020
&lt;/PRE&gt;
&lt;P&gt;As you can see, all values are padded with blanks to the defined length, and the use of the $CHARw. informat preserves leading blanks. With the standard $w. informat, leading blanks would be discarded.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jun 2019 09:14:36 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-06-20T09:14:36Z</dc:date>
    <item>
      <title>Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567544#M159608</link>
      <description>&lt;P&gt;Hi, the case is that I have a data (one column csv file), but some values ending with a trailing space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use proc import to get it in to SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Issue is that I need to use the strings with space, but it always gets imported with no space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It guesses the max length properly, but when retrieving the data, it always trim the space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, it's saved to the dataset without the trailing space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;anyone can help here?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 07:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567544#M159608</guid>
      <dc:creator>AhmedAccordBGro</dc:creator>
      <dc:date>2019-06-20T07:13:09Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567545#M159609</link>
      <description>&lt;P&gt;Welcome to the SAS Community.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use a data step to import CSV files instead. Do a Google search or search this community. Plenty of code examples out there &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 07:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567545#M159609</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-06-20T07:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567549#M159613</link>
      <description>&lt;P&gt;Thanks for welcoming me aboard:)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried dataset as well, and same issue, for two days i'm exploring the internet for a solution, so I decided to ask here..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;below is my code,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sample data "ABDCEF" that got processed normally, but like "ABCDEF " (with trailing space) it got trimmed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data IMPORT;&lt;BR /&gt;infile "/folders/myfolders/ecsql193/DM.csv"&lt;BR /&gt;delimiter = ','&lt;BR /&gt;missover&lt;BR /&gt;firstobs = 2&lt;BR /&gt;DSD&lt;BR /&gt;lrecl = 32767;&lt;BR /&gt;Length File_Name $12.;&lt;BR /&gt;Length Worksheet_Name $12.;&lt;BR /&gt;Length Start_Position $2.;&lt;BR /&gt;Length End_Position $4.;&lt;BR /&gt;Length Structure $8;&lt;BR /&gt;Length File_Process_Flag $8;&lt;BR /&gt;Length KPI_Process_Flag $31;&lt;BR /&gt;Length Fact_Name $103;&lt;BR /&gt;Length F_DIM_Category $35;&lt;BR /&gt;Length F_DIM_Product_Group $6;&lt;BR /&gt;Length F_DIM_Product $21;&lt;BR /&gt;Length F_DIM_Subject_Area $22;&lt;BR /&gt;Length DIM_Month $1;&lt;BR /&gt;Length DIM_RatePlan_Type $1;&lt;BR /&gt;Length DIM_Cust_Seg $1;&lt;BR /&gt;Length DIM_Traffic_Type $1;&lt;BR /&gt;Length DIM_BB_Spd $1;&lt;BR /&gt;Length DIM_BB_Technology $1;&lt;BR /&gt;Length DIM_TER_NW__FLAG_Y_N_ $1;&lt;BR /&gt;Length DIM_USG_TIME__Flag_Y_N_ $1;&lt;BR /&gt;Length DIM_BNDL_Flag__PAYG_Flag_Y_N_ $1;&lt;BR /&gt;Length DIM_REV_CAT $1;&lt;BR /&gt;Length DIM_ROAM_CNTY $1;&lt;BR /&gt;Length DIM_DATA_BUNDLE $1;&lt;BR /&gt;Length Measures $26;&lt;BR /&gt;Length Conditions $17;&lt;BR /&gt;input&lt;BR /&gt;File_Name&lt;BR /&gt;Worksheet_Name&lt;BR /&gt;Start_Position&lt;BR /&gt;End_Position&lt;BR /&gt;Structure&lt;BR /&gt;File_Process_Flag&lt;BR /&gt;KPI_Process_Flag&lt;BR /&gt;Fact_Name&lt;BR /&gt;F_DIM_Category&lt;BR /&gt;F_DIM_Product_Group&lt;BR /&gt;F_DIM_Product&lt;BR /&gt;F_DIM_Subject_Area&lt;BR /&gt;DIM_Month&lt;BR /&gt;DIM_RatePlan_Type&lt;BR /&gt;DIM_Cust_Seg&lt;BR /&gt;DIM_Traffic_Type&lt;BR /&gt;DIM_BB_Spd&lt;BR /&gt;DIM_BB_Technology&lt;BR /&gt;DIM_TER_NW__FLAG_Y_N_&lt;BR /&gt;DIM_USG_TIME__Flag_Y_N_&lt;BR /&gt;DIM_BNDL_Flag__PAYG_Flag_Y_N_&lt;BR /&gt;DIM_REV_CAT&lt;BR /&gt;DIM_ROAM_CNTY&lt;BR /&gt;DIM_DATA_BUNDLE&lt;BR /&gt;Measures&lt;BR /&gt;Conditions&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 07:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567549#M159613</guid>
      <dc:creator>AhmedAccordBGro</dc:creator>
      <dc:date>2019-06-20T07:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567553#M159615</link>
      <description>&lt;P&gt;Once a character variable is defined with a certain length, it will always be padded up to that length with blanks. A missing character variable contains only blanks.&lt;/P&gt;
&lt;P&gt;So if your character column has the correct length, everything's OK.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 08:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567553#M159615</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-20T08:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567554#M159616</link>
      <description>&lt;P&gt;What if data looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ABC&lt;/P&gt;&lt;P&gt;&amp;nbsp; DEF&lt;/P&gt;&lt;P&gt;XYZ&amp;nbsp; (two trailing spaces)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to handle this?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 08:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567554#M159616</guid>
      <dc:creator>AhmedAccordBGro</dc:creator>
      <dc:date>2019-06-20T08:23:25Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567558#M159618</link>
      <description>&lt;P&gt;See this code as an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile datalines dlm="," dsd;
input number string:$char12.;
test_of_string = put(string,$hex24.);
datalines4;
1,ABC
2,  DEF
3,XYZ
;;;;

proc print data=test noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;number    string         test_of_string

   1      ABC       414243202020202020202020
   2        DEF     202044454620202020202020
   3      XYZ       58595A202020202020202020
&lt;/PRE&gt;
&lt;P&gt;As you can see, all values are padded with blanks to the defined length, and the use of the $CHARw. informat preserves leading blanks. With the standard $w. informat, leading blanks would be discarded.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 09:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567558#M159618</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-20T09:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567561#M159620</link>
      <description>&lt;P&gt;Thanks man, but if you add extra space after row#3, you'll get my point exactly. spaces are trimmed, and I need to preserve them!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;infile datalines dlm="," dsd;&lt;BR /&gt;input number string:$char12.;&lt;BR /&gt;test_of_string = put(string,$hex24.);&lt;BR /&gt;datalines4;&lt;BR /&gt;1,ABC&lt;BR /&gt;2, ABC&lt;BR /&gt;3,ABC&amp;nbsp; &amp;nbsp;&lt;BR /&gt;;;;;&lt;/P&gt;&lt;P&gt;proc print data=test noobs;&lt;/P&gt;&lt;P&gt;%PUT&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 09:30:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567561#M159620</guid>
      <dc:creator>AhmedAccordBGro</dc:creator>
      <dc:date>2019-06-20T09:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567564#M159621</link>
      <description>&lt;P&gt;Look at the test variable. Hex 20's ARE blanks.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 09:40:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567564#M159621</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-20T09:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567566#M159622</link>
      <description>&lt;P&gt;Thanks buddy anyways, i got stuck and I think I should handle it out of SAS, on the data level.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 10:07:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567566#M159622</guid>
      <dc:creator>AhmedAccordBGro</dc:creator>
      <dc:date>2019-06-20T10:07:42Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567570#M159623</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/249822"&gt;@AhmedAccordBGro&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks man, but if you add extra space after row#3, you'll get my point exactly. spaces are trimmed, and I need to preserve them!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;infile datalines dlm="," dsd;&lt;BR /&gt;input number string:$char12.;&lt;BR /&gt;test_of_string = put(string,$hex24.);&lt;BR /&gt;datalines4;&lt;BR /&gt;1,ABC&lt;BR /&gt;2, ABC&lt;BR /&gt;3,ABC&amp;nbsp; &amp;nbsp;&lt;BR /&gt;;;;;&lt;/P&gt;
&lt;P&gt;proc print data=test noobs;&lt;/P&gt;
&lt;P&gt;%PUT&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;PS what is that %PUT doing in there?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 10:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567570#M159623</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-20T10:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567572#M159625</link>
      <description>&lt;P&gt;yes I need to preserve the blank space,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I even executed below query, and found the space is trimmed!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;BR /&gt;Select Length(string) From test;&lt;BR /&gt;Quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Results in 3, 4, 3&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 10:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567572#M159625</guid>
      <dc:creator>AhmedAccordBGro</dc:creator>
      <dc:date>2019-06-20T10:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567575#M159626</link>
      <description>&lt;P&gt;Read the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0f6jve4kdxnh1n1m7c82fhosgih.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;documentation of the length() function&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Returns the length of a non-blank character string, &lt;STRONG&gt;excluding trailing blanks&lt;/STRONG&gt;, and returns 1 for a blank character string.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;(emphasis by me)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 11:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567575#M159626</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-20T11:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a char variable of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567602#M159632</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/249822"&gt;@AhmedAccordBGro&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I see your point: You need to preserve the blanks contained in your CSV file because they carry valuable information, e.g., &lt;FONT face="courier new,courier"&gt;'XYZ'&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;' XYZ'&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;'XYZ '&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;'XYZ&amp;nbsp; &amp;nbsp; &amp;nbsp;'&lt;/FONT&gt; must later be recognized as &lt;EM&gt;different&lt;/EM&gt; character values in the SAS dataset. Normally this would fail because of the padding (trailing) blanks which are contained in every character variable of sufficient length: You couldn't distinguish between &lt;EM&gt;these&lt;/EM&gt; blanks and the &lt;EM&gt;original&lt;/EM&gt;&amp;nbsp;blanks from the CSV file as they would look exactly the same (characters with ASCII code [decimal] 32, i.e. hex 20).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a possible solution you could replace the original blanks by a suitable replacement character which does not occur in the CSV file, perhaps the "non-breakable space" with ASCII code 160 (hex A0), while reading the CSV file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create a text file for demonstration */

filename test temp;

data _null_;
file test;
put 'ABC' / ' DEF' / 'XYZ  ' / '2 words';
run;

/* Read the text file preserving the blanks contained 
   (as protected blanks, hex A0) */

data want;
infile test;
input @;
_infile_=translate(_infile_,'A0'x,' ');
input c :$6.;
run;

/* Show the result */

proc print data=want;
format c $hex12.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    c

 1     414243202020
 2     A04445462020
 3     58595AA0A020
 4     32A0776F7264&lt;/PRE&gt;
&lt;P&gt;As you can see, leading blanks (obs. 2), trailing blanks (obs. 3) and embedded blanks (obs. 4) can still be recognized in character variable C (by their hex code A0) and distinguished from those trailing blanks (hex 20) which SAS introduced as usual (such as the last blank in obs. 3 and the trailing blanks in obs. 1 and 2).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This approach should work for a comma-delimited text file as well.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 13:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-char-variable-of-different-lengths/m-p/567602#M159632</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-06-20T13:09:53Z</dc:date>
    </item>
  </channel>
</rss>

