<?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: substr function question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338140#M76911</link>
    <description>&lt;P&gt;Once again you completely answered my question. Thank you Art!&lt;/P&gt;</description>
    <pubDate>Sat, 04 Mar 2017 19:32:09 GMT</pubDate>
    <dc:creator>smw10</dc:creator>
    <dc:date>2017-03-04T19:32:09Z</dc:date>
    <item>
      <title>substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338133#M76904</link>
      <description>&lt;P&gt;Good afternoon everyone. I have two questions about the substring function. I'm looking at a Base SAS certification study guide and there are two questions I got wrong. Additionally, this particular study guide does not explain the answers. The first question pertains to the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; first="Ipswich, England";&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; City=substr(first,1,7);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; City_Country=City!!", "!!"England";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why wouldn't the value for City_Country be "Ipswich, England."? I thought the substr was taking out 7 characters from the first observation which would be Ipswich. However, the value ends up being "Ipswich , England"; notice the space after "Ipswich."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My second question pertains to the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; first="Ipswich, England";&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; City_Country=substr(first,1,7)!!", "!!"England";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why wouldn't the length of City_Country be 16? In the previous example, the result of City_Country is "Ipswich , England" which is 16 characters. Shouldn't the value of City_Country be the same in this example. The length ends up being 25 in this example.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 18:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338133#M76904</guid>
      <dc:creator>smw10</dc:creator>
      <dc:date>2017-03-04T18:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338138#M76909</link>
      <description>&lt;P&gt;If you look carefully at the results of your first test, there is more than one space after Ipswich..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you didn't specify a length for City, it takes the length from First (i.e., 16). Then, when you create City_Country, it takes the length of City (16)+2 (the comma and space)+7 (the length of 'England")=25.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;City_Country gets a value of Ipswich + the 9 spaces that exist to the right of the value Ipswich + the comma and space + the value 'England'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your second example, it use same logic to determine&amp;nbsp;the length, however it only concatenates the first seven characters from first, adds the comma and space, followed by 'England'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 19:22:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338138#M76909</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-04T19:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338139#M76910</link>
      <description>&lt;P&gt;The default length of a variable depends on its type and SAS platform;&lt;/P&gt;
&lt;P&gt;As you did not define length for CITY, it is probably greater then 7, therfore is the space between preceeding the comma.&lt;/P&gt;
&lt;P&gt;In order to get the format 'city, country' use the strip function or catx function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
    first="Ipswich, England";
    City=substr(first,1,7);
    City_Country=strip(City) !! ", " !! "England";
run;
/***** OR ****/
data test1;
    first="Ipswich, England";
    City=substr(first,1,7);
    City_Country=catx(', ',City, "England");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 19:30:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338139#M76910</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-03-04T19:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338140#M76911</link>
      <description>&lt;P&gt;Once again you completely answered my question. Thank you Art!&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 19:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338140#M76911</guid>
      <dc:creator>smw10</dc:creator>
      <dc:date>2017-03-04T19:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338145#M76915</link>
      <description>&lt;P&gt;Art, after playing around with concatenation more, I noticed some strange results. For the following code, the value of concat1 has a lot of spaces between first and second. Upon further inspection, I found that the length of first is 4 and the length of second is 12. Thus, second is the culprit. I added the line "length second 4." before "second=1001" and still the length ends up being 12. The only way I found to fix this is using the "strip" function around second within the concat1 assignment statement. Why does it set the defualt of this numeric as 12? I thought numerics have a default of 8 bytes. Additionally, why can't I change the length with a length statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data concat2;&lt;BR /&gt;first="1568";&lt;BR /&gt;X=length(first);&lt;BR /&gt;second=1001;&lt;BR /&gt;Y=length(second);&lt;BR /&gt;concat1=first||second;&lt;BR /&gt;Z=length(concat1);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 19:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338145#M76915</guid>
      <dc:creator>smw10</dc:creator>
      <dc:date>2017-03-04T19:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338146#M76916</link>
      <description>&lt;P&gt;Thank you for showing me the strip function &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 19:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338146#M76916</guid>
      <dc:creator>smw10</dc:creator>
      <dc:date>2017-03-04T19:56:47Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338154#M76920</link>
      <description>&lt;P&gt;Not strange. In your latest example you're concatenating a string with a length of 4 with a number. When SAS converts a number to a character string it uses 12 bytes to represent the number. If the number has more than 12 characters, it is converted to an E form to fit into the 12 characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you concatenated second with second it would result in the variable having a length of 24.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 20:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338154#M76920</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-04T20:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338155#M76921</link>
      <description>&lt;P&gt;The variable SECOND in your program is a number. So it's default length would by 8, but since it just contains an integer cutting the length in half to 4 would not change how the number is stored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason that it ends up taking 12 characters in your generated string is because SAS will use the BEST12. format to convert the number SECOND into a string. It needs to do this since you used in a place where a character value is expected in the LENGHT() function and with the concatenate operator. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to convert it to a four character string then you could use the PUT() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;concat2=first||put(second,4.);
Z2=length(concat2));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Mar 2017 20:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338155#M76921</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-03-04T20:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: substr function question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338157#M76922</link>
      <description>&lt;P&gt;This makes sense. Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 04 Mar 2017 20:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-function-question/m-p/338157#M76922</guid>
      <dc:creator>smw10</dc:creator>
      <dc:date>2017-03-04T20:45:12Z</dc:date>
    </item>
  </channel>
</rss>

