<?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: Extracting substring from middle of string that's always before a certain expression (e.g., 'vs' in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373110#M11308</link>
    <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data one; 
  infile cards truncover;
input name $1-100; 
cards;
race6            1 Am. Indian   vs 7 White
race6            2 Asian        vs 7 White
marital2         1 Single                     vs 4 Married
;

data want (drop=name x);;
  set one;
  variable=scan(name,1);
  name=left(substr(name,length(variable)+1));
  x=index(name,'vs');
  level=strip(substr(name,1,x-1));
  output;
  level=substr(name,x+2);
  output;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Wed, 05 Jul 2017 00:27:21 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-07-05T00:27:21Z</dc:date>
    <item>
      <title>Extracting substring from middle of string that's always before a certain expression (e.g., 'vs')</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373108#M11307</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have used proc surveylogistic and ods output to extract information from my analysis so that I may manipulate my output into a nice, clean dataset to print out. However, I'm having trouble extracting string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have something like this *see the picture attached called "effect.JPG"*&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to split that column up into 2, where I have it look like the 2 columns in "effect WANT.JPG"*&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;How can I do this? &amp;nbsp;I need a combination of substr, index,&amp;nbsp;COMPBL, scan etc. functions and I'm not sure. I was able to get the first column, call it "variable," by doing this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;spaceplace = index(effect, ' ');&lt;BR /&gt;variable= substr(effect,1,spaceplace);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I need help getting the class level name (labeled as "Level of CLASS Variable for 1 Variable" in the effect2 WANT.JPG file). Basically, what I need to do is to extract the string that comes after the first word, first spaces, but is before the "[trailing blanks] vs [string]"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I tell sas to give me the word that comes right before the "vs" string? I tried to write out a code for you guys to play around with, but it is not working 100% for me &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; But maybe you can modify this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data one; &lt;BR /&gt;input name $1-100; &lt;BR /&gt;cards;&lt;BR /&gt;'race6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 Am. Indian &amp;nbsp; vs 7 White'&lt;BR /&gt;'race6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 Asian &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;vs 7 White'&lt;BR /&gt;'marital2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 Single &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; vs 4 Married'&lt;BR /&gt;'lengthdeployment 1 Less than 1 month vs 6 I did not deploy in the past 12 months'&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data two;&lt;BR /&gt; set one;&lt;BR /&gt; spaceplace = index(effect, ' ');&lt;BR /&gt; variable = substr(effect,1,spaceplace);&lt;/P&gt;
&lt;P&gt;*The above two lines gave me my variable column and it worked in my main code. Not sure if it'll work here though;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*This is my closest attempt to getting it. but it is still wrong and gives me the vs [text] for some of them:;&lt;/P&gt;
&lt;P&gt;vsplace = index(effect, ' vs ');&lt;/P&gt;
&lt;P&gt;CLASSNAME3 = COMPBL(strip(substr(effect, spaceplace, vsplace-5)));&lt;BR /&gt;&lt;BR /&gt;run; &lt;BR /&gt; &lt;BR /&gt;proc print; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in my case, I'd want to have classname3 look like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 Am. Indian&lt;BR /&gt;2 Asian&lt;BR /&gt;1 Single&lt;BR /&gt;1 Less than 1 month&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it'd just be easier to look at my "effect2 want.jpg" to see what I am trying to achieve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for your help. Yes, I have looked through the message boards for similar searches but am still having trouble. I hope you're having a nice 4th of July!&lt;/P&gt;
&lt;P&gt;Best,&lt;BR /&gt;Gina&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/14033i5BBA603CE9BD8511/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="effect.JPG" title="effect.JPG" /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/14034iE241206F58DAAEA1/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="effect2 WANT.JPG" title="effect2 WANT.JPG" /&gt;</description>
      <pubDate>Tue, 04 Jul 2017 23:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373108#M11307</guid>
      <dc:creator>ginak</dc:creator>
      <dc:date>2017-07-04T23:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring from middle of string that's always before a certain expression (e.g., 'vs'</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373110#M11308</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data one; 
  infile cards truncover;
input name $1-100; 
cards;
race6            1 Am. Indian   vs 7 White
race6            2 Asian        vs 7 White
marital2         1 Single                     vs 4 Married
;

data want (drop=name x);;
  set one;
  variable=scan(name,1);
  name=left(substr(name,length(variable)+1));
  x=index(name,'vs');
  level=strip(substr(name,1,x-1));
  output;
  level=substr(name,x+2);
  output;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 00:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373110#M11308</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-05T00:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting substring from middle of string that's always before a certain expression (e.g., 'vs'</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373111#M11309</link>
      <description>&lt;P&gt;I love playing with character functions!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this moves the yardsticks for you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; one;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;infile&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;cards&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;dlm&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'09'x&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;length&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; name $&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;100&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; name;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;cards&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;race6 1 Am. Indian vs 7 White&lt;/P&gt;
&lt;P&gt;race6 2 Asian vs 7 White&lt;/P&gt;
&lt;P&gt;marital2 1 Single vs 4 Married&lt;/P&gt;
&lt;P&gt;lengthdeployment 1 Less than 1 month vs 6 I did not deploy in the past 12 months&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; want(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;drop&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;=_:);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;length&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; variable classname3 _RemainderOfString $&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;100&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; one;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt; variable = scan(name, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;" "&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;); &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;/* Get the first word, blank delimited */&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt; _FirstBlank = find(name, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;" "&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;); &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;/* Find the first blank */&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt; _RemainderOfString = strip(substr(name, _FirstBlank)); &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;/* Get everything after the first word, and strip out leading blanks */&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt; _RemainderOfString = tranwrd(_RemainderOfString, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;' vs '&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'09'x&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;); &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;/* Turn all instances of blank followed by "vs" followed by blank into a tab character */&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt; classname3 = scan(_RemainderOfString, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;, &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'09'x&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;); &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;/* Get the first work, tab delimited */&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 00:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extracting-substring-from-middle-of-string-that-s-always-before/m-p/373111#M11309</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2017-07-05T00:30:33Z</dc:date>
    </item>
  </channel>
</rss>

