<?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: scan last 3 words in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556151#M154892</link>
    <description>&lt;P&gt;%let first=%sysfunc(substr(&amp;amp;prodpro,index(&amp;amp;prodpro,scan(&amp;amp;prodpro,3,'\'))));&lt;BR /&gt;%put &amp;amp;first;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;its giving me error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Argument 2 to function SUBSTR referenced by the %SYSFUNC or %QSYSFUNC macro function is&lt;BR /&gt;not a number.&lt;BR /&gt;ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution&lt;BR /&gt;of %SYSCALL statement or %SYSFUNC or %QSYSFUNC function reference is terminated.&lt;/P&gt;</description>
    <pubDate>Sat, 04 May 2019 07:13:54 GMT</pubDate>
    <dc:creator>sam1231</dc:creator>
    <dc:date>2019-05-04T07:13:54Z</dc:date>
    <item>
      <title>scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556149#M154890</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;c:\xx\my\dc\output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want to scan last 3 words&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;expected output:&lt;/P&gt;&lt;P&gt;newvar=my\dc\output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can i do that?&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 06:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556149#M154890</guid>
      <dc:creator>sam1231</dc:creator>
      <dc:date>2019-05-04T06:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556150#M154891</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
path='c:\xx\my\dc\output';
newvar=substr(path,index(path,scan(path,3,'\')));
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 May 2019 07:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556150#M154891</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-04T07:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556151#M154892</link>
      <description>&lt;P&gt;%let first=%sysfunc(substr(&amp;amp;prodpro,index(&amp;amp;prodpro,scan(&amp;amp;prodpro,3,'\'))));&lt;BR /&gt;%put &amp;amp;first;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;its giving me error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Argument 2 to function SUBSTR referenced by the %SYSFUNC or %QSYSFUNC macro function is&lt;BR /&gt;not a number.&lt;BR /&gt;ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution&lt;BR /&gt;of %SYSCALL statement or %SYSFUNC or %QSYSFUNC function reference is terminated.&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 07:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556151#M154892</guid>
      <dc:creator>sam1231</dc:creator>
      <dc:date>2019-05-04T07:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556153#M154894</link>
      <description>&lt;P&gt;Please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let prodpro=c:\xx\my\dc\output;
%let first=%substr(&amp;amp;prodpro,%index(&amp;amp;prodpro,%scan(&amp;amp;prodpro,3,\)));
%put &amp;amp;first;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 May 2019 07:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556153#M154894</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-04T07:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556158#M154899</link>
      <description>Try:&lt;BR /&gt;&lt;BR /&gt;%let p = c:\xx\my\dc\output;&lt;BR /&gt;%let last3 =&lt;BR /&gt;%scan(&amp;amp;p,-3,\)\%scan(&amp;amp;p,-2,\)\%scan(&amp;amp;p,-1,\);</description>
      <pubDate>Sat, 04 May 2019 08:19:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556158#M154899</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-04T08:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556159#M154900</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/243302"&gt;@sam1231&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the number of words in the path (&lt;FONT face="courier new,courier"&gt;&amp;amp;prodpro&lt;/FONT&gt;) may vary (e.g.&amp;nbsp;&lt;FONT face="courier new,courier"&gt;c:\xx\yy\zz\dc\output&lt;/FONT&gt;) or if duplicate words may be involved (e.g.&amp;nbsp;&lt;FONT face="courier new,courier"&gt;c:\xx\xx\dc\output&lt;/FONT&gt;), I would prefer&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let first=%scan(&amp;amp;prodpro,-3,\)\%scan(&amp;amp;prodpro,-2,\)\%scan(&amp;amp;prodpro,-1,\);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: I see, Astounding was faster. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 08:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556159#M154900</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-04T08:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556164#M154903</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_; 
x='c:\xx\my\dc\output';
call scan(x,-3,p,l,'\');
want=substr(x,p);
put x= want=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 May 2019 11:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556164#M154903</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-04T11:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556170#M154907</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/243302"&gt;@sam1231&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Scan from end of string to cope with a different number of path elements. Using -3 will give the third element seen from end of string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If data step functions are used in macro code, as you do in your first answer, all functions must be inclosed in %sysfunc, not the whole expression, as shown in the second example.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are 4 different ways of getting the last 3 elements. Note that only the data step is "secure", the other 3 will give a wrong result if there are several occurences of the &lt;U&gt;same directory name&lt;/U&gt; in the path.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited because&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;pointed out a major flaw in the code. it worked not&amp;nbsp;on 'c:\amylase\yy\my\dc\output', because the index function found 'my' in 'amylase'. This is fixed so index is searching for '\my\'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Macro code */
%let var = c:\amylase\yy\my\dc\output;

* with macro functions;
%let newvar = %substr(&amp;amp;var,%index(&amp;amp;var,\%scan(&amp;amp;var,-3,\)\)+1);
%put &amp;amp;=newvar;

* with sysfunc + data step functions;
%let newvar = %sysfunc(substr(&amp;amp;var,%sysfunc(index(&amp;amp;var,\%sysfunc(scan(&amp;amp;var,-3,\))\))+1));
%put &amp;amp;=newvar;

/* From existing data */
data have;
	var = 'c:\amylase\yy\my\dc\output'; 
run;

* Data step;
data want; set have;
	call scan(var,-3,p,l,'\');
	newwar = substr(var,p);
run;

* Proc sql;
proc sql;
	create table want2 as select 
		var, 
		substr(var,index(var,'\'||scan(var,-3,'\')||'\')+1) as newvar
	from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 13:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556170#M154907</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-05-04T13:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556173#M154909</link>
      <description>&lt;P&gt;The risk of using %index remains that the word is found earlier than intended, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var = c:\amylase\my\dc\output;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 13:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556173#M154909</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-04T13:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556179#M154911</link>
      <description>Thang you - You are so right! - I have edited the post to make it better, but call scan seems to be the only secure method. There must be som way to do something similar in macro code or sql expressions, but I havn't found it yet.</description>
      <pubDate>Sat, 04 May 2019 14:02:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556179#M154911</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-05-04T14:02:22Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556187#M154919</link>
      <description>&lt;P&gt;Yes, even "\&lt;EM&gt;word&lt;/EM&gt;\" could be found prematurely if it occurs more than once (as in&amp;nbsp;&lt;FONT face="courier new,courier"&gt;c:\xx\xx\dc\output&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a macro version of the CALL SCAN solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=-3;
%let p=.;
%let l=.;
%let d=\;
%syscall scan(prodpro,n,p,l,d);
%let first=%substr(&amp;amp;prodpro,&amp;amp;p);

%put &amp;amp;=first;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Sat, 04 May 2019 14:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556187#M154919</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-04T14:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: scan last 3 words</title>
      <link>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556197#M154926</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just came to think of another method to avoid the index function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	var = 'c:\aa\bb\ff\tt\dir';
	newvar = prxchange('s/(.*\\)(\w*\\\w*\\\w*$)/$2/',-1,trim(var));
	put newvar;
run;

%let var = c:\aa\bb\ff\tt\dir;
%let newvar = %sysfunc(prxchange(s/(.*\\)(\w*\\\w*\\\w*$)/$2/,-1,&amp;amp;var));
%put &amp;amp;newvar;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 May 2019 16:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/scan-last-3-words/m-p/556197#M154926</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-05-04T16:36:38Z</dc:date>
    </item>
  </channel>
</rss>

