<?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 find the position of a character in string based on a certain condition? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531533#M145486</link>
    <description>Hi:&lt;BR /&gt;  Are your variable strings always separated by --- as you show, or is that just fake filler that you added? It appears that the blue X is a different number of positions away from the red X? Is that correct? Or did you mis-count and the blue X and red X are always 17 or 18 positions apart? How did you get the position of the red X into  a variable named POS?&lt;BR /&gt;&lt;BR /&gt;What code have you tried? What functions have you tried. The answer will be different whether your strings are separated by ---- or not.&lt;BR /&gt;&lt;BR /&gt;Cynthia</description>
    <pubDate>Thu, 31 Jan 2019 02:45:36 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2019-01-31T02:45:36Z</dc:date>
    <item>
      <title>How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531510#M145471</link>
      <description>&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;Let's say I have observations with a variable string as such (it differs across observations):&lt;/P&gt;&lt;P&gt;Person A) .&amp;nbsp; &amp;nbsp;------XXX------&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;X&lt;/FONT&gt;&lt;/STRONG&gt;XXXXXXXXXXXXXXXX&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;XXX-------&lt;/P&gt;&lt;P&gt;Person B) .&amp;nbsp; &amp;nbsp;------&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;X&lt;/FONT&gt;&lt;/STRONG&gt;XXXXXXXXXXXXXXXXXXXX&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;XXX-------&lt;/P&gt;&lt;P&gt;Person C) .&amp;nbsp; &amp;nbsp;------XXX---XXXXXXXXXX----&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;X&lt;/FONT&gt;&lt;/STRONG&gt;XXXXXXXXXXXXXXX&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;XXX-------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the position number of the red &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt; and have defined this position number as POS.&lt;/P&gt;&lt;P&gt;How do I use POS to find the position of the blue &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 00:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531510#M145471</guid>
      <dc:creator>soomx</dc:creator>
      <dc:date>2019-01-31T00:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531527#M145482</link>
      <description>&lt;P&gt;So you're looking for the start of the substring?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have the delimiters shown, the ---? If so, I would consider modifying this a bit to use the SCAN() or you could just loop through and check each character until you find the first - and then use that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to confirm the structure of your data first though.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/203140"&gt;@soomx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;Let's say I have observations with a variable string as such (it differs across observations):&lt;/P&gt;
&lt;P&gt;Person A) .&amp;nbsp; &amp;nbsp;------XXX------&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;X&lt;/FONT&gt;&lt;/STRONG&gt;XXXXXXXXXXXXXXXX&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;XXX-------&lt;/P&gt;
&lt;P&gt;Person B) .&amp;nbsp; &amp;nbsp;------&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;X&lt;/FONT&gt;&lt;/STRONG&gt;XXXXXXXXXXXXXXXXXXXX&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;XXX-------&lt;/P&gt;
&lt;P&gt;Person C) .&amp;nbsp; &amp;nbsp;------XXX---XXXXXXXXXX----&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;X&lt;/FONT&gt;&lt;/STRONG&gt;XXXXXXXXXXXXXXX&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;XXX-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the position number of the red &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt; and have defined this position number as POS.&lt;/P&gt;
&lt;P&gt;How do I use POS to find the position of the blue &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;X&lt;/STRONG&gt;&lt;/FONT&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 02:42:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531527#M145482</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-31T02:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531530#M145485</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  A='------123-----Aabcdefghijklmnopqrsx--------'; POS=25; output;
  A='----BXXXXXXXXXXXXXXXXXXXXXXXX-------       '; POS=20; output;
  A='----XXX---XXXXXXXXXX----CXXXXXXXXXXXXXXXX--'; POS=40; output;
run;
data WANT;
  set HAVE;          
  POS2=findc(reverse(A),'-',length(A)-POS)-1;
  B=char(reverse(A),POS2); 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;A&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;POS&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;POS2&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;B&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;------123-----&lt;STRONG&gt;A&lt;/STRONG&gt;abcdefghijklmnopqrsx--------&lt;/TD&gt;
&lt;TD class="r data"&gt;25&lt;/TD&gt;
&lt;TD class="r data"&gt;29&lt;/TD&gt;
&lt;TD class="l data"&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;----&lt;STRONG&gt;B&lt;/STRONG&gt;XXXXXXXXXXXXXXXXXXXXXXXX-------&lt;/TD&gt;
&lt;TD class="r data"&gt;20&lt;/TD&gt;
&lt;TD class="r data"&gt;39&lt;/TD&gt;
&lt;TD class="l data"&gt;B&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;----XXX---XXXXXXXXXX----&lt;STRONG&gt;C&lt;/STRONG&gt;XXXXXXXXXXXXXXXX--&lt;/TD&gt;
&lt;TD class="r data"&gt;40&lt;/TD&gt;
&lt;TD class="r data"&gt;19&lt;/TD&gt;
&lt;TD class="l data"&gt;C&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 02:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531530#M145485</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-01-31T02:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531533#M145486</link>
      <description>Hi:&lt;BR /&gt;  Are your variable strings always separated by --- as you show, or is that just fake filler that you added? It appears that the blue X is a different number of positions away from the red X? Is that correct? Or did you mis-count and the blue X and red X are always 17 or 18 positions apart? How did you get the position of the red X into  a variable named POS?&lt;BR /&gt;&lt;BR /&gt;What code have you tried? What functions have you tried. The answer will be different whether your strings are separated by ---- or not.&lt;BR /&gt;&lt;BR /&gt;Cynthia</description>
      <pubDate>Thu, 31 Jan 2019 02:45:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531533#M145486</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-01-31T02:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531538#M145489</link>
      <description>&lt;P&gt;This might be brutal and inelegant, but it should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;if POS &amp;gt; 1 then do k=POS-1 to 1 by -1 until (substr(string, k, 1) ne "X");&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;first_x = k + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 02:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531538#M145489</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-31T02:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531598#M145529</link>
      <description>&lt;P&gt;I would use CALL SCAN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input key $10. string $50.;
cards;
Person A) ------XXX------XXXXXXXXXXXXXXXXXXyXX-------
Person B) ------XXXXXXXXXXXXXXXXXXXXXXyXX-------
Person C) ------XXX---XXXXXXXXXX----XXXXXXXXXXXXXXXXXyXX-------
;run;

data want;
  set have;
  pos=index(string,'y');
  do _N_=1 by 1 until(pos2&amp;lt;pos&amp;lt;pos2+len or pos2=0);
    call scan(string,_N_,pos2,len,'-');
    end;
  drop len;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The position of the first letter in the word containing the letter "y" (a substitute for the red "X") should then be in the variable pos2.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 10:34:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531598#M145529</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-01-31T10:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the position of a character in string based on a certain condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531722#M145591</link>
      <description>&lt;P&gt;Thank you! This worked perfectly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To answer the other questions (apologies for not being more clear):&lt;/P&gt;&lt;P&gt;The variable string have values of dashes and Xs (i.e. the dashes are not fake fillers). Observations have differing lengths and patterns of Xs and dashes. These dashes and Xs represent presence at a specific visit/time point (akin to yes/no present), and the position of each character represents which time point. I have the variable POS to indicate the position of the red X because that is my time point of interest. I wanted to find the position of the blue X to find the starting point in which observations had continuous presence at each visit until the visit of interest. Hopefully that is more clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 17:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-position-of-a-character-in-string-based-on-a/m-p/531722#M145591</guid>
      <dc:creator>soomx</dc:creator>
      <dc:date>2019-01-31T17:19:18Z</dc:date>
    </item>
  </channel>
</rss>

