<?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: using array and substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282735#M57499</link>
    <description>Thank you all for the replies! all codes are helpful.</description>
    <pubDate>Thu, 07 Jul 2016 16:46:04 GMT</pubDate>
    <dc:creator>lillymaginta</dc:creator>
    <dc:date>2016-07-07T16:46:04Z</dc:date>
    <item>
      <title>using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282054#M57274</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; patientid    &lt;SPAN class="token number"&gt;dx&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;dx&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;dx&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;dx&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;4&lt;/SPAN&gt;   &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;datalines&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;1               250 223 224 444    5/5/2009 
1               555 666 120 2501    5/6/2008 
2               120  666  .  .     1/2/2007 
2               120  666  .  .     1/1/2007 
3               250   .   .  .     2/2/2004 
3               240 2502   .  .     3/3/2004 
3               2503  .    .  .     1/1/2004 &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi, I have the following database; &amp;nbsp;I want to create the following code: if substr for any of the four variables (dx1-dx4) starts with 250** (first three codes 250) then db=1, else db=0; output would be:&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1               250 223 224 444    5/5/2009  1
1               555 666 120 2501    5/6/2008 1
2               120  666  .  .     1/2/2007  0
2               120  666  .  .     1/1/2007  0
3               250   .   .  .     2/2/2004  1
3               240 2502   .  .     3/3/2004  1
3               2503  .    .  .     1/1/2004  1&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Jul 2016 21:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282054#M57274</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-07-04T21:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282056#M57275</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Rather than using character functions , can do it with a a numeric expression&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;data want;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;set have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;array dx dx:;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;db=0;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;do over dx;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;if int(dx/(10**(int(log10(dx)-2 ))))=250 then do; db=1;leave;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Alternatively , use character funcions&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;if substr(left(put(dx,best32.)),1,3)='250' then do; db=1;leave;end;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jul 2016 23:00:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282056#M57275</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2016-07-04T23:00:05Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282062#M57276</link>
      <description>&lt;P&gt;Thank you for posting the code. Using the first one, let's assume I want to capture the first 4 numbers instead of the first 3 from the left &amp;nbsp;(2501 instead of only 250) in that case do I need to change anything in your code?&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jul 2016 23:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282062#M57276</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-07-04T23:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282067#M57277</link>
      <description>&lt;P&gt;It would be a lot easy , if you read them as Character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input patientid    (dx1 dx2 dx3 dx4)  ($);
datalines;
1               250 223 224 444    5/5/2009 
1               555 666 120 2501    5/6/2008 
2               120  666  .  .     1/2/2007 
2               120  666  .  .     1/1/2007 
3               250   .   .  .     2/2/2004 
3               240 2502   .  .     3/3/2004 
3               2503  .    .  .     1/1/2004 
;
run;

data want;
 set have;
 array x{*} $ dx:;
 flag=0;
 do i=1 to dim(x);
  if x{i} =: '250' /*Change it 2501*/ then do;flag=1;leave;end;
 end;
 drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jul 2016 00:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282067#M57277</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-05T00:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282068#M57278</link>
      <description>&lt;P&gt;I'm general, I highly recommend storing diagnosis codes as characters rather than numbers. You'll never do math with them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally most coding systems have letters included, ICD9 and ICD10.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2016 00:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282068#M57278</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-05T00:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282072#M57279</link>
      <description>&lt;P&gt;For legibility and speed I'd write this as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data HAVE;
   input PATIENTID (DX1 - DX4)  ($);
datalines;
1               250 223 224 444    5/5/2009 
1               555 666 120 2501    5/6/2008 
2               120  666  .  .     1/2/2007 
2               120  666  .  .     1/1/2007 
3               250   .   .  .     2/2/2004 
3               240 2502   .  .     3/3/2004 
3               2503  .    .  .     1/1/2004 
;
run;

data WANT;
 set HAVE;
 FLAG=( DX1=:'250' | DX2=:'250' | DX3=:'250' | DX4=:'250' ); 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variables have to be numeric then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data HAVE;
   input PATIENTID DX1 - DX4  ;
datalines;
1               250 223 224 444    5/5/2009 
1               555 666 120 2501    5/6/2008 
2               120  666  .  .     1/2/2007 
2               120  666  .  .     1/1/2007 
3               250   .   .  .     2/2/2004 
3               240 2502   .  .     3/3/2004 
3               2503  .    .  .     1/1/2004 
;
run;

data WANT;
 set HAVE;
 FLAG=( cat(DX1)=:'250' | cat(DX2)=:'250' | cat(DX3)=:'250' | cat(DX4)=:'250' ); 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jul 2016 02:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282072#M57279</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-07-05T02:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282075#M57280</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SUBSTRN can be helpful in this situation:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; input patientid dx1 dx2 dx3 dx4 date :mmddyy10.;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 250 223 224 444 5/5/2009 &lt;BR /&gt;1 555 666 120 2501 5/6/2008 &lt;BR /&gt;2 120 666 . . 1/2/2007 &lt;BR /&gt;2 120 666 . . 1/1/2007 &lt;BR /&gt;3 250 . . . 2/2/2004 &lt;BR /&gt;3 250 2502 . . 3/3/2004 &lt;BR /&gt;3 2503 . . . 1/1/2004 &lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(drop=i);&lt;BR /&gt; set have;&lt;BR /&gt; array x{*} dx:;&lt;BR /&gt; db=0;&lt;BR /&gt; do i=1 to dim(x);&lt;BR /&gt; if SUBSTRN(x{i},1,3) = '250' then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;db=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;leave;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2016 04:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282075#M57280</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2016-07-05T04:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282100#M57290</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/72105"&gt;@lillymaginta﻿&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;With regards to&amp;nbsp;your question about adapting the numeric expression to read '2501' , the answer is to use one of the the character options. The numeric solution was just interesting to me for another problem , but now I've learned about diagnostic codes from this thread I can see it wasn't suitable here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2016 08:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282100#M57290</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2016-07-05T08:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282308#M57365</link>
      <description>&lt;P&gt;This will also&amp;nbsp;avoid writing a loop and deals with both numbers and strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WANT;
  set HAVE;
  FLAG=prxmatch('/\b250/',catx(' ', of DX1 - DX4 )) &amp;gt; 0; *find a word starting with 250;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2016 22:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282308#M57365</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-07-05T22:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: using array and substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282735#M57499</link>
      <description>Thank you all for the replies! all codes are helpful.</description>
      <pubDate>Thu, 07 Jul 2016 16:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-and-substr/m-p/282735#M57499</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2016-07-07T16:46:04Z</dc:date>
    </item>
  </channel>
</rss>

