<?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 Use of array and substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410805#M100390</link>
    <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS Q1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16456i4361F29165FDA400/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS Q1.PNG" alt="SAS Q1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi guys!!&lt;BR /&gt;&lt;BR /&gt;Need your help on understanding the usage of an array with substring for the above codes.&lt;BR /&gt;The above screenshot was taken from a video where I am doing some self-learning for SAS programming.&lt;BR /&gt;The first data step created two strings: "abcdefghij" and "klmnopqrst";&lt;/P&gt;&lt;P&gt;while the second data step aims to separate&amp;nbsp;the two string into its respective components of "a", "b", "c" ... etc.&lt;BR /&gt;&lt;BR /&gt;However, I do not understand why the Do statement of "Do j=2 to 11;" is used instead of "Do j=1 to 10;"?&lt;BR /&gt;I have tried using&amp;nbsp;&lt;SPAN&gt;"Do j=1 to 10;" and I know that the output generated will be wrong with the first record "a" being written over.&lt;BR /&gt;&lt;/SPAN&gt;But I am not sure what is the rationale behind this.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks in advance to anyone that can provide some clarification on the above question!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2017 11:33:12 GMT</pubDate>
    <dc:creator>FaithInFate</dc:creator>
    <dc:date>2017-11-06T11:33:12Z</dc:date>
    <item>
      <title>Use of array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410805#M100390</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS Q1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16456i4361F29165FDA400/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS Q1.PNG" alt="SAS Q1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi guys!!&lt;BR /&gt;&lt;BR /&gt;Need your help on understanding the usage of an array with substring for the above codes.&lt;BR /&gt;The above screenshot was taken from a video where I am doing some self-learning for SAS programming.&lt;BR /&gt;The first data step created two strings: "abcdefghij" and "klmnopqrst";&lt;/P&gt;&lt;P&gt;while the second data step aims to separate&amp;nbsp;the two string into its respective components of "a", "b", "c" ... etc.&lt;BR /&gt;&lt;BR /&gt;However, I do not understand why the Do statement of "Do j=2 to 11;" is used instead of "Do j=1 to 10;"?&lt;BR /&gt;I have tried using&amp;nbsp;&lt;SPAN&gt;"Do j=1 to 10;" and I know that the output generated will be wrong with the first record "a" being written over.&lt;BR /&gt;&lt;/SPAN&gt;But I am not sure what is the rationale behind this.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks in advance to anyone that can provide some clarification on the above question!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2017 11:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410805#M100390</guid>
      <dc:creator>FaithInFate</dc:creator>
      <dc:date>2017-11-06T11:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: Use of array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410810#M100392</link>
      <description>&lt;P&gt;It's because of the array declaration.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array a{*} _character_ s1-s10;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which actually means&lt;/P&gt;
&lt;P&gt;"create an array with all &lt;EM&gt;existing&lt;/EM&gt; character variables and new variables s1 to s10, and set the dimension accordingly"&lt;/P&gt;
&lt;P&gt;Since the variable string already exists when the array is defined, it ends up as a{1}, and needs to be skipped.&lt;/P&gt;
&lt;P&gt;To clarify, add this to the second datastep:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x1 = vname(a{1});&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and after testing that, try a slightly modified step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
array a{*} $1 s1-s10;
set old;
u = string;
do j = 1 to 10;
  a{j} = substr(u,j,1);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2017 12:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410810#M100392</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-06T12:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Use of array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410814#M100393</link>
      <description>&lt;P&gt;You say you got this as a training video, where did you get it from as it seems to show some bad methodology there.&amp;nbsp; For instance:&lt;BR /&gt;- Why take 16 characters in the input string, if you know you are only dealing with 10 characters.&amp;nbsp; This suggests to me problems for the future - i.e. you would miss 6 characters if the string was longer.&lt;/P&gt;
&lt;P&gt;- As above, hardcoded limit of characters in array but not in input.&lt;/P&gt;
&lt;P&gt;- Why would the array need to keep _character_ as well??&amp;nbsp; That is asking for problems, add one more character to the dataset for instance and your program will not give you the intended result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code can be dissolved to:&lt;/P&gt;
&lt;PRE&gt;data old;
  input string$1-16;
datalines;
abcdefghij
klmnopqrst
;
run;
data new;
  set old;
  array s{10} $1;
  do i=1 to 10;
    s{i}=char(string,i);
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Depending on the length of the string in, you may want to take max(lengthn(string)) and use that rather than assuming 10 characters.&amp;nbsp; Also, avoid the whole mixed case coding, its hard to read.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2017 12:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-array-and-substr/m-p/410814#M100393</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-06T12:29:07Z</dc:date>
    </item>
  </channel>
</rss>

