<?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: Iterate Character range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822827#M324904</link>
    <description>My task does indeed go beyond the characters that I mentioned. But, it seems all the solutions are doing this (including this one) in a way which requires to put in the datalines or do it in informat step. Is there any way of incorporating this solution to an existing dataset (with millions of obs for example) and does not require creating the dataset? Thank you very much.</description>
    <pubDate>Tue, 12 Jul 2022 13:11:44 GMT</pubDate>
    <dc:creator>nonlinear999</dc:creator>
    <dc:date>2022-07-12T13:11:44Z</dc:date>
    <item>
      <title>Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822463#M324786</link>
      <description>&lt;P&gt;Hi Guys&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have got a dataset that looks like the following -&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
   input ric $ price;
datalines;
"BCDCA" 10 
"BCDCZ" 12 
"ABX"   4	
"ABCBA" 50	
"ABCBZ" 60	
run;&lt;/PRE&gt;&lt;P&gt;I want to iterate over the ric (which is a character variable) and flag it if the substring after 3 characters = "CA - CZ", "BA-BZ". Here, CA - CZ includes everything in between (i.e., CA, CB, CC, CD etc. ) and same with BA-BZ. For example, if the substring of the ric is&amp;nbsp;&lt;/P&gt;&lt;P&gt;like the&lt;/P&gt;&lt;PRE&gt;if substrn(ric,4,2)="CA-CZ" then flag=1; else flag=0 &lt;/PRE&gt;&lt;P&gt;I know I can't put the condition like this but want to know if there is any way of doing sth like this. Rather than typing it individually (my dataset is huge and has a lot of ric codes) if there is a sas way of doing that, that would save me a huge amount time. Thank you very much.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jul 2022 12:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822463#M324786</guid>
      <dc:creator>nonlinear999</dc:creator>
      <dc:date>2022-07-10T12:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822467#M324788</link>
      <description>&lt;P&gt;How about an INFORMAT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   invalue test 'CA'-'CZ' = 1 'BA'-'BZ' = 2;
   quit;

data have;
   input ric :$quote. price;
   x = substr(ric,4);
   flag = input(x,test.);
   datalines;
"BCDCA" 10 
"BCDCZ" 12 
"ABX"   4	
"ABCBA" 50	
"ABCBZ" 60	
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 183px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73110i34F6FBDDC716AEA1/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Jul 2022 13:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822467#M324788</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2022-07-10T13:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822468#M324789</link>
      <description>data have;&lt;BR /&gt;   input ric :$quote20. price;&lt;BR /&gt;   if "CA"&amp;lt;=substrn(ric,4,2)&amp;lt;="CZ" then flag=1; else flag=0;&lt;BR /&gt;datalines;&lt;BR /&gt;"BCDCA" 10 &lt;BR /&gt;"BCDCZ" 12 &lt;BR /&gt;"ABX"   4	&lt;BR /&gt;"ABCBA" 50	&lt;BR /&gt;"ABCBZ" 60&lt;BR /&gt;;</description>
      <pubDate>Sun, 10 Jul 2022 13:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822468#M324789</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-07-10T13:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822478#M324791</link>
      <description>&lt;P&gt;If your task goes beyond looking for trailing BA...BZ&amp;nbsp; or CA...CZ, but rather&amp;nbsp; &amp;nbsp;!A through !Z (where ! is any capital letter), then you can check for capital A through Z in the 4th and 5th characters then use the "rank" of the 4th character to produce a flag=1 (for AA-AZ), or 2 (BA-BZ), .... or 26 (for ZA-ZZ).&amp;nbsp; I've added a few more RIC codes as examples:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ric :$quote20. price;
  if ('A'&amp;lt;=char(ric,4)&amp;lt;='Z') and ('A'&amp;lt;=char(ric,5)&amp;lt;='Z')
     then flag=rank(substr(ric,4,1))-rank('A')+1;
  if _n_=1 then put " RIC  PRICE  FLAG";
  put ric @6 price 5. +4 flag 2.;
datalines;
"BCDCA" 10
"BCDCZ" 12
"ABX" 4
"ABCBA" 50
"ABCBZ" 60
"ABCxZ" 101
"DEFA4" 102
"EFGZC" 104
"EFGQa" 105
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which produces the following on the sas log:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;651  data have;
652    input ric :$quote20. price;
653    if ('A'&amp;lt;=char(ric,4)&amp;lt;='Z') and ('A'&amp;lt;=char(ric,5)&amp;lt;='Z')
654       then flag=rank(substr(ric,4,1))-rank('A')+1;
655    if _n_=1 then put " RIC  PRICE  FLAG";
656    put ric @6 price 5. +4 flag 2.;
657  datalines;

 RIC  PRICE  FLAG
BCDCA   10     3
BCDCZ   12     3
ABX      4     .
ABCBA   50     2
ABCBZ   60     2
ABCxZ  101     .
DEFA4  102     .
EFGZC  104    26
EFGQa  105     .
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want flag=0 for non-qualifiers (instead of flag=. above), just prefix the if statement with&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  flag=0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 10 Jul 2022 19:30:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822478#M324791</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-10T19:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822827#M324904</link>
      <description>My task does indeed go beyond the characters that I mentioned. But, it seems all the solutions are doing this (including this one) in a way which requires to put in the datalines or do it in informat step. Is there any way of incorporating this solution to an existing dataset (with millions of obs for example) and does not require creating the dataset? Thank you very much.</description>
      <pubDate>Tue, 12 Jul 2022 13:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/822827#M324904</guid>
      <dc:creator>nonlinear999</dc:creator>
      <dc:date>2022-07-12T13:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/823198#M325046</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/352863"&gt;@nonlinear999&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;My task does indeed go beyond the characters that I mentioned. But, it seems all the solutions are doing this (including this one) in a way which requires to put in the datalines or do it in informat step. Is there any way of incorporating this solution to an existing dataset (with millions of obs for example) and does not require creating the dataset? Thank you very much.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Existing dataset?&amp;nbsp; You could avoid creating a dataset &lt;EM&gt;&lt;U&gt;&lt;STRONG&gt;file&lt;/STRONG&gt;&lt;/U&gt;&lt;/EM&gt;, and just create a dataset &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;view&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;.&amp;nbsp; The advantage is that you can use the code as in my suggestion, but avoid taking up disk space with a new file.. Moreover, the dataset view can be made permanent, so it's ready for reuse without being re-programmed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need / view=need;
  set have;
  flag=0;
  if ('A'&amp;lt;=char(ric,4)&amp;lt;='Z') and ('A'&amp;lt;=char(ric,5)&amp;lt;='Z')
    then flag=rank(substr(ric,4,1))-rank('A')+1;
run;
proc freq data=need;
  tables flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2022 18:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/823198#M325046</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-13T18:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate Character range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/823343#M325086</link>
      <description>Sorry, my understanding of the suggested solutions was poor so thought I needed to create the datalines. All of the solutions actually works. Thanks heaps.</description>
      <pubDate>Thu, 14 Jul 2022 15:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-Character-range/m-p/823343#M325086</guid>
      <dc:creator>nonlinear999</dc:creator>
      <dc:date>2022-07-14T15:40:03Z</dc:date>
    </item>
  </channel>
</rss>

