<?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: Create a data set with char var in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/648054#M194038</link>
    <description>&lt;P&gt;Length and Informat while related are not the same thing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Especially in the world (old I know but still valid) of fixed column input.&lt;/P&gt;
&lt;P&gt;There are times when you may have to read data with a different informat for one file even while setting the length longer than the actual data.&lt;/P&gt;
&lt;P&gt;Consider these three data steps that attempt to read a fixed column data where the first three characters should be read into one variable and the next 3 into a different numeric variable.&lt;/P&gt;
&lt;PRE&gt;data example;
   length x $ 20;
   input x  y;
datalines;
abc123
;

data example2;
   length x $ 20;
   input x @4  y;
datalines;
abc123
;


data example3;
   length x $ 20;
   input x $3.  y;
datalines;
abc123
;
&lt;/PRE&gt;
&lt;P&gt;One issue with assigning longer length than needed is the results of some functions will pad the result with the missing characters. Consider the following code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   length x $ 20;
   x='abc';
   y = quote(x);
   put y=;
run;&lt;/PRE&gt;
&lt;P&gt;When you run the above step the log will show a result of&lt;/P&gt;
&lt;PRE&gt;y="abc                 "

&lt;/PRE&gt;
&lt;P&gt;Notice all of the spaces after the c before the closing quote character.&lt;/P&gt;
&lt;P&gt;This will happen with a large number of character functions.&lt;/P&gt;
&lt;P&gt;Which is why the Strip() function (or older combination of trim(left(var)) ) is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final bit is "know thy data". If you have a document describing a data source that indicates the longest value that will be in a field then I would follow that document.&lt;/P&gt;
&lt;P&gt;Sometimes the whole process is iterative because your data isn't documented and you have to make some guesses. Some of those guesses will be wrong and you may have to go back and change things to accommodate later knowledge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 May 2020 14:31:48 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-05-15T14:31:48Z</dc:date>
    <item>
      <title>Create a data set with char var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/647986#M193989</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I am creating a data set .&lt;/P&gt;
&lt;P&gt;I want to ask a few questions:&lt;/P&gt;
&lt;P&gt;1-W is a char var.&lt;/P&gt;
&lt;P&gt;Is it mandatory to put $&amp;nbsp; in input statement after var name w?&lt;/P&gt;
&lt;P&gt;I am using length&amp;nbsp; statement so&amp;nbsp; I think that then it is not essential to add $ in input statement&lt;/P&gt;
&lt;P&gt;2-In length statement should it be written as $4. or $4 or $ 4 (with space between $ and 4)?&lt;/P&gt;
&lt;P&gt;3-In which case we use length statement when we create a char var?&lt;/P&gt;
&lt;P&gt;Is it essential to use length statement or just if the char var is long?(what is minimum length of char var that is the required to define it on&amp;nbsp;length&amp;nbsp; statement?)&lt;/P&gt;
&lt;P&gt;4-How do you know what is the required&amp;nbsp;length&amp;nbsp; that can be used?&lt;/P&gt;
&lt;P&gt;Should I count letters in the longest value and then put this number in&amp;nbsp;length&amp;nbsp; statement?&lt;/P&gt;
&lt;P&gt;What happen if for example the required&amp;nbsp;length is 4 but I define&amp;nbsp;length&amp;nbsp; of 100?&lt;/P&gt;
&lt;P&gt;Is it just a loose of memory?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA tbl;
Length W  $ 4;
input X   W;
cards;
789 1234
009 0009
1 9999
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 May 2020 10:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/647986#M193989</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-05-15T10:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create a data set with char var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/647992#M193994</link>
      <description>&lt;P&gt;Here are some answers:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If you have defined the variable as character, you can omit the format.&lt;/LI&gt;
&lt;LI&gt;All three are valid, I normally use $4 (no period, as that makes it look like a format).&lt;/LI&gt;
&lt;LI&gt;Length statements are used when you want to define a length different from the one set by SAS as default (the various functions have default lengths, sometimes depending on the input variables). E.g. S=substr(longstring,3,2) will give S the same length as LONGSTRING, so you may want to set the length to 2 first. When using only $ (no informat) as the input modifier, SAS defaults to a length of 8.&lt;/LI&gt;
&lt;LI&gt;If you use Options COMPRESS=CHAR, the waste of disk space from having strings "too long" is minimal. Memory is normally not a problem in these situations. So set the length so long that you are sure they are long enough, and don't waste too much time counting characters.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 15 May 2020 11:00:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/647992#M193994</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-15T11:00:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create a data set with char var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/648054#M194038</link>
      <description>&lt;P&gt;Length and Informat while related are not the same thing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Especially in the world (old I know but still valid) of fixed column input.&lt;/P&gt;
&lt;P&gt;There are times when you may have to read data with a different informat for one file even while setting the length longer than the actual data.&lt;/P&gt;
&lt;P&gt;Consider these three data steps that attempt to read a fixed column data where the first three characters should be read into one variable and the next 3 into a different numeric variable.&lt;/P&gt;
&lt;PRE&gt;data example;
   length x $ 20;
   input x  y;
datalines;
abc123
;

data example2;
   length x $ 20;
   input x @4  y;
datalines;
abc123
;


data example3;
   length x $ 20;
   input x $3.  y;
datalines;
abc123
;
&lt;/PRE&gt;
&lt;P&gt;One issue with assigning longer length than needed is the results of some functions will pad the result with the missing characters. Consider the following code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   length x $ 20;
   x='abc';
   y = quote(x);
   put y=;
run;&lt;/PRE&gt;
&lt;P&gt;When you run the above step the log will show a result of&lt;/P&gt;
&lt;PRE&gt;y="abc                 "

&lt;/PRE&gt;
&lt;P&gt;Notice all of the spaces after the c before the closing quote character.&lt;/P&gt;
&lt;P&gt;This will happen with a large number of character functions.&lt;/P&gt;
&lt;P&gt;Which is why the Strip() function (or older combination of trim(left(var)) ) is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final bit is "know thy data". If you have a document describing a data source that indicates the longest value that will be in a field then I would follow that document.&lt;/P&gt;
&lt;P&gt;Sometimes the whole process is iterative because your data isn't documented and you have to make some guesses. Some of those guesses will be wrong and you may have to go back and change things to accommodate later knowledge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 14:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-data-set-with-char-var/m-p/648054#M194038</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-15T14:31:48Z</dc:date>
    </item>
  </channel>
</rss>

