<?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: Want to create macro in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390863#M3318</link>
    <description>&lt;P&gt;The macro processor is a tool to write dynamic code, not for working with data.&lt;/P&gt;
&lt;P&gt;The macro statements will work while the code is fetched, not while procedures or data steps are running.&lt;/P&gt;
&lt;P&gt;Only a few of the data step functions have a macro-style equivalent.&lt;/P&gt;
&lt;P&gt;Let's take a look at the log (see Maxim 2):&lt;/P&gt;
&lt;PRE&gt;24         %macro wordparsin(word);
25         data test;
26         i = 1;
27         /* word = 'I am using scan and index function in sas together and I got successful'; */
28         %do %while (%find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0);
29         w = %scan(&amp;amp;word,i);
30         output;
31         i+1;
32         %end;
33         keep w i;
34         run;
35         %mend;
36         
37         %wordparsin(I am using scan and index function in sas together);
WARNING: Apparent invocation of macro FIND not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       i 
ERROR: Argument 2 to macro function %SCAN is not a number.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       %find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0 
ERROR: The condition in the %DO %WHILE loop, %find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0, yielded an invalid or missing value, .  The macro 
       will stop executing.
ERROR: The macro WORDPARSIN will stop executing.
38         
39         GOPTIONS NOACCESSIBLE;
40         %LET _CLIENTTASKLABEL=;
41         %LET _CLIENTPROJECTPATH=;
42         %LET _CLIENTPROJECTNAME=;
43         %LET _SASPROGRAMFILE=;
44         
45         ;*';*";*/;quit;run;
                     ____
                     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST may be incomplete.  When this step was stopped there were 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;The first WARNING alerts you to the fact that there is no %find macro function.&lt;/P&gt;
&lt;P&gt;The&amp;nbsp; first ERROR comes from the fact that the missing %find function produce some character value&lt;/P&gt;
&lt;P&gt;The next ERROR from the %scan function comes from the fact that the macro processor finds the &lt;STRONG&gt;TEXT(!)&lt;/STRONG&gt; i, which is not a number; note that variable i will only exist once the data step runs, not while the macro &lt;STRONG&gt;PRE&lt;/STRONG&gt;processor executes the macro.&lt;/P&gt;
&lt;P&gt;And so on.&lt;/P&gt;
&lt;P&gt;Keep your data step logic, and only make the input (the string) variable by replacing it with the macro parameter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro wordparsin(word);
data test;
i = 1;
word = "&amp;amp;word";
do while (find(word,scan(word,i),'t')&amp;gt;0);
  w = scan(word,i);
  output;
  i+1;
end;
keep w i;
run;
%mend;

%wordparsin(I am using scan and index function in sas together);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Aug 2017 09:41:36 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-08-25T09:41:36Z</dc:date>
    <item>
      <title>Want to create macro</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390861#M3317</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have written a code in base sas to extract all the words in a string/paragraph.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a macro for it. But I failed. Requesting help. Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below are the Codes which is successfully running and the macro code which is not. Please advise.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;=======running=========================================&lt;BR /&gt;data test;&lt;BR /&gt;i = 1;&lt;BR /&gt;word = 'I am using scan and index function in sas together and I got successful';&lt;BR /&gt;do while (find(word,scan(word,i),'t')&amp;gt;0);&lt;BR /&gt;w = scan(word,i);&lt;BR /&gt;output;&lt;BR /&gt;i+1;&lt;BR /&gt;end;&lt;BR /&gt;keep w i;&lt;BR /&gt;run;&lt;BR /&gt;===============================================&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;==========Error==============================&lt;BR /&gt;%macro wordparsin(word);&lt;BR /&gt;data test;&lt;BR /&gt;i = 1;&lt;BR /&gt;/* word = 'I am using scan and index function in sas together and I got successful'; */&lt;BR /&gt;%do %while (%find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0);&lt;BR /&gt;w = %scan(&amp;amp;word,i);&lt;BR /&gt;output;&lt;BR /&gt;i+1;&lt;BR /&gt;%end;&lt;BR /&gt;keep w i;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;%wordparsin(I am using scan and index function in sas together);&lt;BR /&gt;========================================&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 09:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390861#M3317</guid>
      <dc:creator>SRakesh</dc:creator>
      <dc:date>2017-08-25T09:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Want to create macro</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390863#M3318</link>
      <description>&lt;P&gt;The macro processor is a tool to write dynamic code, not for working with data.&lt;/P&gt;
&lt;P&gt;The macro statements will work while the code is fetched, not while procedures or data steps are running.&lt;/P&gt;
&lt;P&gt;Only a few of the data step functions have a macro-style equivalent.&lt;/P&gt;
&lt;P&gt;Let's take a look at the log (see Maxim 2):&lt;/P&gt;
&lt;PRE&gt;24         %macro wordparsin(word);
25         data test;
26         i = 1;
27         /* word = 'I am using scan and index function in sas together and I got successful'; */
28         %do %while (%find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0);
29         w = %scan(&amp;amp;word,i);
30         output;
31         i+1;
32         %end;
33         keep w i;
34         run;
35         %mend;
36         
37         %wordparsin(I am using scan and index function in sas together);
WARNING: Apparent invocation of macro FIND not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       i 
ERROR: Argument 2 to macro function %SCAN is not a number.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       %find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0 
ERROR: The condition in the %DO %WHILE loop, %find(&amp;amp;word,%scan(&amp;amp;word,i),'t')&amp;gt;0, yielded an invalid or missing value, .  The macro 
       will stop executing.
ERROR: The macro WORDPARSIN will stop executing.
38         
39         GOPTIONS NOACCESSIBLE;
40         %LET _CLIENTTASKLABEL=;
41         %LET _CLIENTPROJECTPATH=;
42         %LET _CLIENTPROJECTNAME=;
43         %LET _SASPROGRAMFILE=;
44         
45         ;*';*";*/;quit;run;
                     ____
                     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST may be incomplete.  When this step was stopped there were 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;The first WARNING alerts you to the fact that there is no %find macro function.&lt;/P&gt;
&lt;P&gt;The&amp;nbsp; first ERROR comes from the fact that the missing %find function produce some character value&lt;/P&gt;
&lt;P&gt;The next ERROR from the %scan function comes from the fact that the macro processor finds the &lt;STRONG&gt;TEXT(!)&lt;/STRONG&gt; i, which is not a number; note that variable i will only exist once the data step runs, not while the macro &lt;STRONG&gt;PRE&lt;/STRONG&gt;processor executes the macro.&lt;/P&gt;
&lt;P&gt;And so on.&lt;/P&gt;
&lt;P&gt;Keep your data step logic, and only make the input (the string) variable by replacing it with the macro parameter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro wordparsin(word);
data test;
i = 1;
word = "&amp;amp;word";
do while (find(word,scan(word,i),'t')&amp;gt;0);
  w = scan(word,i);
  output;
  i+1;
end;
keep w i;
run;
%mend;

%wordparsin(I am using scan and index function in sas together);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 09:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390863#M3318</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-25T09:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Want to create macro</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390867#M3319</link>
      <description>&lt;P&gt;As a tip, you can use countw() function for you loop and thus drop the other code:&lt;/P&gt;
&lt;PRE&gt;data test;
  word = 'I am using scan and index function in sas together and I got successful';
  do i=1 to countw(word);
    w=scan(word,i);
    output;
  end;
  keep w i;
run;&lt;/PRE&gt;
&lt;P&gt;And for your macro, just replace the word= line and pop macro text around it (though I don't see much point in the code).&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 09:51:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390867#M3319</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-08-25T09:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Want to create macro</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390870#M3320</link>
      <description>&lt;P&gt;You may want to parse a long string into its words, in a macro, then the code&lt;/P&gt;
&lt;P&gt;would be similar to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro parse(string);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%do i = 1 %to %sysfunc(countw(&amp;amp;staring));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %let word = %scan(&amp;amp;string,i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %put I=&amp;amp;i &amp;nbsp;WORD=&amp;amp;word;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;%parse(what wonderful world); &amp;nbsp; &amp;nbsp;/* try it */&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 10:37:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390870#M3320</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-08-25T10:37:05Z</dc:date>
    </item>
    <item>
      <title>Re: Want to create macro</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390884#M3321</link>
      <description>&lt;P&gt;Thanks Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is detaied explanation... and got to understand things very clearly...&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 12:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390884#M3321</guid>
      <dc:creator>SRakesh</dc:creator>
      <dc:date>2017-08-25T12:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Want to create macro</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390886#M3322</link>
      <description>Thanks,&lt;BR /&gt;&lt;BR /&gt;Learn new functions as well...</description>
      <pubDate>Fri, 25 Aug 2017 12:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Want-to-create-macro/m-p/390886#M3322</guid>
      <dc:creator>SRakesh</dc:creator>
      <dc:date>2017-08-25T12:24:03Z</dc:date>
    </item>
  </channel>
</rss>

