<?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: how to extract initial character from the character variable using array ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451812#M113977</link>
    <description>&lt;P&gt;Thanks, this also worked perfectly!!!&lt;/P&gt;</description>
    <pubDate>Fri, 06 Apr 2018 09:39:38 GMT</pubDate>
    <dc:creator>mehul4frnds</dc:creator>
    <dc:date>2018-04-06T09:39:38Z</dc:date>
    <item>
      <title>how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451531#M113852</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to extract initial one character from the character variables using an array&amp;nbsp;and create two new&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;variable&amp;nbsp;(D, E).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data look like this&lt;/P&gt;&lt;P&gt;data have1;&lt;BR /&gt;input id name$ gen$ PROCHI DATE A B C;&lt;BR /&gt;datalines;&lt;BR /&gt;1 m_ram male 1 1.1 1 . 1.23456&lt;BR /&gt;2 m_mohan male 1 1.1 99 1 1.23456&lt;BR /&gt;3 m_shyam male 2 2.1 1 99 1.23456&lt;BR /&gt;4 f_radha female 2 2.1 90 1 99&lt;BR /&gt;5 f_gita female 2 2.1 99 95 1.23456&lt;BR /&gt;6 f_sita female 2 3.1 . 1 1.23456&lt;BR /&gt;7 f_ranu female 3 2.1 1 . 1.23456&lt;BR /&gt;8 f_tina female 3 2.1 . 1 1.23456&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want data like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;id&amp;nbsp; name$&amp;nbsp; &amp;nbsp; &amp;nbsp;gen$&amp;nbsp; &amp;nbsp; &amp;nbsp;ID&amp;nbsp; &amp;nbsp; DATE A&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D&amp;nbsp; &amp;nbsp;E;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 m_ram&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;male&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; 1.1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; 1.23456&amp;nbsp; &amp;nbsp;m m&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 m_mohan&amp;nbsp; male&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; 1.1&amp;nbsp; &amp;nbsp; &amp;nbsp;99&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;1.23456&amp;nbsp; &amp;nbsp;m m&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 m_shyam&amp;nbsp; male&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; 2.1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; 99&amp;nbsp; &amp;nbsp;1.23456&amp;nbsp; &amp;nbsp;m m&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4 f_radha&amp;nbsp; &amp;nbsp; &amp;nbsp; female&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp;2.1&amp;nbsp; &amp;nbsp; 90&amp;nbsp; &amp;nbsp; 1&amp;nbsp; 99&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f&amp;nbsp; &amp;nbsp;f&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;5 f_gita&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;female&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; 2.1&amp;nbsp; &amp;nbsp;99&amp;nbsp; &amp;nbsp;95&amp;nbsp; &amp;nbsp; 1.23456&amp;nbsp; f&amp;nbsp; &amp;nbsp;f&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;6 f_sita&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; female&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; 3.1&amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; 1.23456&amp;nbsp; f&amp;nbsp; &amp;nbsp;f&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;7 f_ranu&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; female&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp;2.1&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp;1.23456&amp;nbsp; f&amp;nbsp; &amp;nbsp;f&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;8 f_tina&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;female&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; 2.1&amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;1.23456 f&amp;nbsp; &amp;nbsp;f&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I have tried following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want (drop=i) ;&lt;BR /&gt;set have1 ;&lt;BR /&gt;array try {2} $ name gen ;&lt;/P&gt;&lt;P&gt;array try2 {i} $ D E;&lt;BR /&gt;do i = 1 to 2;&lt;BR /&gt;try2 {i} = substr {try2{i},1,1} ;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it's not working.&amp;nbsp;It says 'undeclared&amp;nbsp;array referenced'&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;substr&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly suggest&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 14:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451531#M113852</guid>
      <dc:creator>mehul4frnds</dc:creator>
      <dc:date>2018-04-05T14:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451551#M113868</link>
      <description>&lt;P&gt;You referenced the same array, the unpopulated one on both side of the =&lt;/P&gt;
&lt;PRE&gt;try2 {i} = substr {try2{i},1,1}&lt;/PRE&gt;
&lt;P&gt;use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;try2 {i} = substr {try{i},1,1}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451551#M113868</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-05T15:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451553#M113870</link>
      <description>&lt;P&gt;SUBSTR does not use curly brackets.&amp;nbsp; Use parentheses instead.&amp;nbsp; Note that the second array name should be TRY, not TRY2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;try2 {i} = substr (try{i},1,1) ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, get clever and get rid of SUBSTR:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array try2 {i} $&lt;FONT color="#ff0000"&gt;1&lt;/FONT&gt; D E;&lt;BR /&gt;do i = 1 to 2;&lt;BR /&gt;try2 {i} =&amp;nbsp;try{i} ;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the array defines the new variables as 1 character long, you only have room to store the first character.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451553#M113870</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-05T15:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451554#M113871</link>
      <description>&lt;P&gt;Substr is a function, therefore the use of curly braces isn't going to work is it:&lt;/P&gt;
&lt;PRE&gt;try2 {i} = substr {try{i},1,1};&lt;/PRE&gt;
&lt;P&gt;Should it not be:&lt;/P&gt;
&lt;PRE&gt;try2 {i} = substr(try{i},1,1);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451554#M113871</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-05T15:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451557#M113873</link>
      <description>&lt;P&gt;The array and loop is a bit pointless unless learning arrays:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  d=char(name,1);&lt;BR /&gt;  e=char(gen,1);&lt;BR /&gt;run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451557#M113873</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-05T15:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451561#M113874</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; You could do all your manipulation in the original program. I don't actually see that using an ARRAY is necessary for what your stated purpose is. I also don't see the need to read ID and PROCHI with those names if you want PROCHI to be called ID in the final output. You might also use the SCAN function to get rid of the m_ or f_ in front of name (assuming that if you substring it out you want name without that string).&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dont_need_array.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/19646i956B92DE652016FB/image-size/large?v=v2&amp;amp;px=999" role="button" title="dont_need_array.png" alt="dont_need_array.png" /&gt;&lt;/span&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this gives you some suggestions for a different approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451561#M113874</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-04-05T15:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451656#M113909</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Substr is a function, therefore the use of curly braces isn't going to work is it:&lt;/P&gt;
&lt;PRE&gt;try2 {i} = substr {try{i},1,1};&lt;/PRE&gt;
&lt;P&gt;Should it not be:&lt;/P&gt;
&lt;PRE&gt;try2 {i} = substr(try{i},1,1);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Good catch, my monitor resolution makes it hard to see curly braces so I don't use them.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 18:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451656#M113909</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-05T18:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451808#M113973</link>
      <description>&lt;P&gt;Thanks for the reply. This is really a good way to get rid of ARRAY&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 09:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451808#M113973</guid>
      <dc:creator>mehul4frnds</dc:creator>
      <dc:date>2018-04-06T09:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451809#M113974</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thanks, Cynthia for such an insightful reply.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 09:32:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451809#M113974</guid>
      <dc:creator>mehul4frnds</dc:creator>
      <dc:date>2018-04-06T09:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451811#M113976</link>
      <description>&lt;P&gt;Thanks for such an apt solution&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 09:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451811#M113976</guid>
      <dc:creator>mehul4frnds</dc:creator>
      <dc:date>2018-04-06T09:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract initial character from the character variable using array ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451812#M113977</link>
      <description>&lt;P&gt;Thanks, this also worked perfectly!!!&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 09:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-initial-character-from-the-character-variable/m-p/451812#M113977</guid>
      <dc:creator>mehul4frnds</dc:creator>
      <dc:date>2018-04-06T09:39:38Z</dc:date>
    </item>
  </channel>
</rss>

