<?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: Use substring with an array to tag a variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765987#M30619</link>
    <description>&lt;P&gt;Note that you can short circuit the loop once one of the diagnosis codes is found.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag_tag=0;
do i=1 to dim(d_diag) until(diag_tag);
  if d_diag{i} in: ('V12', '250', '414') then diag_tag=1;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Or even more concisely&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to dim(d_diag) until(diag_tag);
  diag_tag = d_diag{i} in: ('V12' '250' '414') ;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 04 Sep 2021 23:39:50 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-09-04T23:39:50Z</dc:date>
    <item>
      <title>Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765943#M30599</link>
      <description>&lt;P&gt;I am trying to use an array to tag various diagnosis variables, but only need the first part of the value to do the tagging. All my variables are in CHAR format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to use the substring function within the array, but it is running into some errors.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Some values of the diag code are V1234, 25011, or 41402. I only need to tag values based on the first three character values: V12, 250, 414.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;/P&gt;&lt;P&gt;Label diag_tag =&amp;nbsp; "diagnosis tag";&lt;/P&gt;&lt;P&gt;array D_DIAG {8} diag_2 - diag_9;&lt;/P&gt;&lt;P&gt;do i = 1 to 8;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if substr(d_diag,1,3) = "V12" then diag_tag = 1 ;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if diag_tag = "" then diag_tag = 0;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Sep 2021 21:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765943#M30599</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-09-03T21:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765956#M30605</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393094"&gt;@pbhatt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to use the substring function within the array, but it is running into some errors.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Whenever you get errors, you need to show us the &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;entire&lt;/STRONG&gt; &lt;/FONT&gt;log for this DATA step (if that's where the error is), or show us the incorrect output (if that's where the error is) and explain what you expect to see.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Sep 2021 22:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765956#M30605</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-03T22:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765960#M30607</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393094"&gt;@pbhatt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to use an array to tag various diagnosis variables, but only need the first part of the value to do the tagging. All my variables are in CHAR format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to use the substring function within the array, but it is running into some errors.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some values of the diag code are V1234, 25011, or 41402. I only need to tag values based on the first three character values: V12, 250, 414.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;/P&gt;
&lt;P&gt;Label diag_tag =&amp;nbsp; "diagnosis tag";&lt;/P&gt;
&lt;P&gt;array D_DIAG {8} diag_2 - diag_9;&lt;/P&gt;
&lt;P&gt;do i = 1 to 8;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if substr(d_diag,1,3) = "V12" then diag_tag = 1 ;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if diag_tag = "" then diag_tag = 0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are not referencing the value of the array variable anywhere. You probably meant&lt;/P&gt;
&lt;PRE&gt; if substr(d_diag[i] ,1,3) = "V12" then diag_tag = 1 ;&lt;/PRE&gt;
&lt;P&gt;if you were attempting to get the first 3 characters of an array element. You need to reference the index (position or however you want to think of it in the array with the loop the counter. That means place the counter in parentheses after the array name. Square brackets also work. I use them so I kind find array index values easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to see if diag_tag was not assigned use the Missing function. You generate a conversion to character note because of comparing the numeric variable to a character string.&lt;/P&gt;
&lt;PRE&gt;if missing( diag_tag)  then diag_tag = 0;&lt;/PRE&gt;
&lt;P&gt;Missing function works with both character and numeric values so you don't have to concern yourself with specific character for comparison. The function returns 1/0 for missing/non-missing and since SAS uses 1 for true and 0 for false you can use it directly with a comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could avoid the possible confusion between index values of 1 to 8 and variable suffixes of 2 to 9 by using&lt;/P&gt;
&lt;PRE&gt;array D_DIAG {2:9} diag_2 - diag_9;

do i = 2 to 9;&lt;/PRE&gt;
&lt;P&gt;When you use the colon between to two values that is lower and upper bound definition which can make some coding much more understandable or easier to debug if the index variable values match the variable suffixes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Sep 2021 22:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765960#M30607</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-03T22:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765972#M30612</link>
      <description>&lt;P&gt;When you want to examine the beginning of a character field (such as the first three characters), you don't need to use SUBSTR.&amp;nbsp; Looking at this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to 8;
   if substr(d_diag,1,3) = "V12" then diag_tag = 1 ;
end;
if diag_tag = "" then diag_tag = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It could be corrected and rewritten in this fashion:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag_tag=0;
do i=1 to 8;
   if d_diag{i} in: ('V12', '250', '414') then diag_tag=1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The colon after the equal sign is a significant character, that limits the comparison to the length of the shorter string (in this case three characters).&lt;/P&gt;</description>
      <pubDate>Fri, 03 Sep 2021 23:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765972#M30612</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-09-03T23:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765987#M30619</link>
      <description>&lt;P&gt;Note that you can short circuit the loop once one of the diagnosis codes is found.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;diag_tag=0;
do i=1 to dim(d_diag) until(diag_tag);
  if d_diag{i} in: ('V12', '250', '414') then diag_tag=1;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Or even more concisely&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to dim(d_diag) until(diag_tag);
  diag_tag = d_diag{i} in: ('V12' '250' '414') ;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Sep 2021 23:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/765987#M30619</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-04T23:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/766002#M30620</link>
      <description>&lt;P&gt;Below just "consolidating" what other's already posted plus a few more tweaks to make your usage of arrays a bit more dynamic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  array diag_ {2:9} $4 (8*'xxxx');
  output;
  diag_5='2509';
  output;
  diag_5='X250';
  output;
run;

data want;
  set have;
  label diag_tag =  "diagnosis tag";
  array d_diag {*} diag_2 - diag_9;
  diag_tag = 0;
  do i = 1 to dim(d_diag);
/*    if substrn(d_diag[i],1,3) in ('V12', '250', '414') then */
    if d_diag[i] in: ('V12', '250', '414') then 
      do;
        diag_tag=1;
        /* no further looping required */
        leave;
      end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&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;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1630727559438.png" style="width: 767px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63291iBF9FDF6F2A897269/image-dimensions/767x115?v=v2" width="767" height="115" role="button" title="Patrick_0-1630727559438.png" alt="Patrick_0-1630727559438.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Variable &lt;EM&gt;&lt;STRONG&gt;i&lt;/STRONG&gt;&lt;/EM&gt; in the 2nd row only got to 4 as that's when the loop could get ended. This saves some processing time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 03:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/766002#M30620</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-04T03:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767152#M30722</link>
      <description>Thanks so much for your detailed response. I went through and made sure to refer to the position of the variable within the array.</description>
      <pubDate>Fri, 10 Sep 2021 23:05:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767152#M30722</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-09-10T23:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767153#M30723</link>
      <description>Thanks for the reply. I looked up how to use the colon after the equal sign and that is an absolute game-changer in identifying a broad set of codes.</description>
      <pubDate>Fri, 10 Sep 2021 23:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767153#M30723</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-09-10T23:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767154#M30724</link>
      <description>Thanks again. This was really helpful as it sped up the processing time for large datasets.</description>
      <pubDate>Fri, 10 Sep 2021 23:11:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767154#M30724</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-09-10T23:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: Use substring with an array to tag a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767155#M30725</link>
      <description>Thanks for consolidating previous replies and providing a visual example. I also read up on arrays to get a better understanding.&lt;BR /&gt;This article was very helpful as well&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/242-30.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/242-30.pdf&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 10 Sep 2021 23:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-substring-with-an-array-to-tag-a-variable/m-p/767155#M30725</guid>
      <dc:creator>pbhatt</dc:creator>
      <dc:date>2021-09-10T23:13:56Z</dc:date>
    </item>
  </channel>
</rss>

