<?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 extract a string based on a word inside the string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391442#M94004</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I believe it's actually quite tricky if you want to build something which also works for multiple case statements in a string especially if you also allow for nested case statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following should work as long as there are no nested case statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let STRMONTH='29Aug2017'd;
%let i=1;

data test;
  test="case when date_account_opened &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_New_aggregate ,case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_Closed_aggregate,case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) else . end as Month_End_Utilisation";

  found=prxmatch('/end\s+as\s+Corp_Closed_aggregate/oi',test);
  if found&amp;gt;0 then
    do;
      start=find(test,'case',-found,'i');
      want=catx(' ',substrn(test,start,found-start),'end as Corp_Closed_aggregate');
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Aug 2017 12:54:44 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2017-08-29T12:54:44Z</dc:date>
    <item>
      <title>How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391425#M94001</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using SAS 9.4. I have the below&amp;nbsp;string stored in a variable called Test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data test;
input Test $1000;
Test="case when date_account_opened &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_New_aggregate ,case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_Closed_aggregate,case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) else . end as Month_End_Utilisation";

Test1=Findw(Test,"Corp_Closed_aggregate");
Test2=Findw(Test,"Case When");&lt;BR /&gt;test3=Substr(Test,Test1,Test2);
Run;

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to extract the complete the case when statement based on the as variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like if I want to search&amp;nbsp;the "&lt;SPAN&gt;Corp_Closed_aggregate", then it should give me the complete "&lt;STRONG&gt;case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_Closed_aggregate&lt;/STRONG&gt;"&amp;nbsp;as result into another variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Could anyone please let me know if it is possible in SAS?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please help me if possible.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 11:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391425#M94001</guid>
      <dc:creator>rajdeep</dc:creator>
      <dc:date>2017-08-29T11:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391428#M94002</link>
      <description>&lt;P&gt;A regular expression would do this easily, but you can just use&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;Test2&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;Findw&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Test&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"Case When"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as you did&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;Test2&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;Findw&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Test&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"End As",'i'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Anything &amp;nbsp;between is the&amp;nbsp;case test and anything after this the variable name&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 11:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391428#M94002</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-08-29T11:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391442#M94004</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I believe it's actually quite tricky if you want to build something which also works for multiple case statements in a string especially if you also allow for nested case statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following should work as long as there are no nested case statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let STRMONTH='29Aug2017'd;
%let i=1;

data test;
  test="case when date_account_opened &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_New_aggregate ,case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_Closed_aggregate,case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) else . end as Month_End_Utilisation";

  found=prxmatch('/end\s+as\s+Corp_Closed_aggregate/oi',test);
  if found&amp;gt;0 then
    do;
      start=find(test,'case',-found,'i');
      want=catx(' ',substrn(test,start,found-start),'end as Corp_Closed_aggregate');
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Aug 2017 12:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391442#M94004</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-29T12:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391444#M94005</link>
      <description>&lt;P&gt;This problem is that your example string includes three CASE statements separated by commas. Easier to see if you refomart the code to show this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Test=cats
("case when date_account_opened &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_New_aggregate"
,','
,"case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_Closed_aggregate"
,','
,"case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) else . end as Month_End_Utilisation"
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the case statements themselves could contain commas you will need to use some type of regular expression to split the string into individual parts. &amp;nbsp;Most likely you will want to use regular expressions for this.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 12:42:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391444#M94005</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-29T12:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391478#M94016</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data test;
infile cards truncover;
input Test $1000.;
temp=substr(test,1,findw(test,'Corp_Closed_aggregate',' ,''i')-1);
want=substr(temp,findw(temp,'case',' ,','ib'));
cards4;
case when date_account_opened &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_New_aggregate ,case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end as Corp_Closed_aggregate,case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) else . end as Month_End_Utilisation
;;;;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Aug 2017 14:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391478#M94016</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-08-29T14:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391541#M94053</link>
      <description>&lt;P&gt;One thing that helps: when providing a data step test that it will run. You had an input statement and nothing to read. Also because you&lt;/P&gt;
&lt;P&gt;have macro variables referenced inside the string you cannot use the " (double quote) or else the macro variables such as &amp;amp;strmonth will attempt to resolve. If you have actually read a text file to contain your "test" variable then that shouldn't be an issue. Note the use of the doubled single quotes around the strings month and same so we can get around that in the example code.&lt;/P&gt;
&lt;P&gt;I truly detest lines of code over 200 characters long displayed on a single line in the editor as they aren't readable and get even worse here so I put in some line breaks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see:&lt;/P&gt;
&lt;PRE&gt;Data test;
Test='case when date_account_opened &amp;gt;= intnx(''month'',&amp;amp;STRMONTH.,&amp;amp;i.,''same'') then 1 
 else 0 end as Corp_New_aggregate ,
 case when closure_date &amp;gt;= intnx(''month'',&amp;amp;STRMONTH.,&amp;amp;i.,''same'') then 1 
 else 0 end as Corp_Closed_aggregate,
 case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) 
 else . end as Month_End_Utilisation';

Test1=Findw(Test,"Corp_Closed_aggregate");
Test2=Findw(Test,"Case When",' ','bi',test1);
test3=Substr(Test,Test2,(test1-test2 +length("Corp_Closed_aggregate")));
Run;&lt;/PRE&gt;
&lt;P&gt;Key pieces: when searching for the "case when" associated with the varaible you search backward from the starting position, so "b" in the findw for test2 and test1 as a start position, the 'i' is included because "Case When" does not exist in your search line so use the 'i' to ignore case. I suggest using the 'i' option in all of the findw calls unless someone is real good about consistent case in that source file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also had the substring for test 3 in the wrong order: Test1 should be &amp;gt; test2. And since test1 and test2 are the positions of the start of the respective found words you need to find the overall number of characters to select, so test1-test2 gives the length of the string from "case" to the start of "Corp_closed_aggregate. So you need to add the length of that variable to get enough characters for the entire value of test3.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 15:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391541#M94053</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-29T15:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a string based on a word inside the string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391769#M94174</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; Not really unless you have nested cases (and then regex probably should be used).&lt;/P&gt;
&lt;P&gt;Otherwise this works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST;
  TEST='case when date_account_opened &amp;gt;= intnx(''month'',&amp;amp;STRMONTH.,&amp;amp;i.,''same'') then 1 
   else 0 end as Corp_New_aggregate ,
   case when closure_date &amp;gt;= intnx(''month'',&amp;amp;STRMONTH.,&amp;amp;i.,''same'') then 1 
   else 0 end as Corp_Closed_aggregate,
   case when a.credit_limit &amp;gt; 0 then (bl.current_balance/a.credit_limit) 
   else . end as Month_End_Utilisation';

  VAR_TO_FIND = 'Corp_Closed_aggregate';
  POSVAR      = find(TEST,' as '||trim(VAR_TO_FIND),'i');
  POSCASE     = find(TEST,'case when ',-POSVAR,'i');
  FORMULA     = substr(TEST,POSCASE,POSVAR-POSCASE);
  putlog FORMULA=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;FORMULA=case when closure_date &amp;gt;= intnx('month',&amp;amp;STRMONTH.,&amp;amp;i.,'same') then 1 else 0 end&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2017 10:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-string-based-on-a-word-inside-the-string/m-p/391769#M94174</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-08-30T10:36:29Z</dc:date>
    </item>
  </channel>
</rss>

