<?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 %do and %scan in maco in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624006#M183744</link>
    <description>&lt;P&gt;I am trying to create a macro that change some words in a sting to lower case. However, only the first word in the list 'Or' was being changed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#333399"&gt;%Macro&lt;/FONT&gt; PCase (Dat=, Var=, LowList=);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data Cased;&lt;BR /&gt;set &amp;amp;Dat;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%let&lt;/FONT&gt; VarCnt=&lt;FONT color="#3366FF"&gt;%sysfunc&lt;/FONT&gt;(countw(&amp;amp;LowList));&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%do&lt;/FONT&gt; i = 1 &lt;FONT color="#3366FF"&gt;%to&lt;/FONT&gt; &amp;amp;VarCnt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%let&lt;/FONT&gt; Var_Ind = &lt;FONT color="#3366FF"&gt;%scan&lt;/FONT&gt;(&amp;amp;LowList,&amp;amp;i," ");&lt;BR /&gt;X1 = tranwrd(X1,&lt;FONT color="#993366"&gt;"&amp;amp;Var_Ind"&lt;/FONT&gt;, lowcase(&lt;FONT color="#993366"&gt;"&amp;amp;Var_Ind"&lt;/FONT&gt;));&lt;BR /&gt;&lt;FONT color="#993366"&gt;%end;&lt;/FONT&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;FONT color="#333399"&gt;%mend&lt;/FONT&gt; PCase;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%let&lt;/FONT&gt; Low = &lt;FONT color="#3366FF"&gt;%str&lt;/FONT&gt;(Or For And And/Or );&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%PCase&lt;/FONT&gt; (Dat=A, Var=Original, LowList=&amp;amp;Low);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Feb 2020 20:33:17 GMT</pubDate>
    <dc:creator>CHELS</dc:creator>
    <dc:date>2020-02-11T20:33:17Z</dc:date>
    <item>
      <title>%do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624006#M183744</link>
      <description>&lt;P&gt;I am trying to create a macro that change some words in a sting to lower case. However, only the first word in the list 'Or' was being changed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#333399"&gt;%Macro&lt;/FONT&gt; PCase (Dat=, Var=, LowList=);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data Cased;&lt;BR /&gt;set &amp;amp;Dat;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%let&lt;/FONT&gt; VarCnt=&lt;FONT color="#3366FF"&gt;%sysfunc&lt;/FONT&gt;(countw(&amp;amp;LowList));&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%do&lt;/FONT&gt; i = 1 &lt;FONT color="#3366FF"&gt;%to&lt;/FONT&gt; &amp;amp;VarCnt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;%let&lt;/FONT&gt; Var_Ind = &lt;FONT color="#3366FF"&gt;%scan&lt;/FONT&gt;(&amp;amp;LowList,&amp;amp;i," ");&lt;BR /&gt;X1 = tranwrd(X1,&lt;FONT color="#993366"&gt;"&amp;amp;Var_Ind"&lt;/FONT&gt;, lowcase(&lt;FONT color="#993366"&gt;"&amp;amp;Var_Ind"&lt;/FONT&gt;));&lt;BR /&gt;&lt;FONT color="#993366"&gt;%end;&lt;/FONT&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;FONT color="#333399"&gt;%mend&lt;/FONT&gt; PCase;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%let&lt;/FONT&gt; Low = &lt;FONT color="#3366FF"&gt;%str&lt;/FONT&gt;(Or For And And/Or );&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%PCase&lt;/FONT&gt; (Dat=A, Var=Original, LowList=&amp;amp;Low);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2020 20:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624006#M183744</guid>
      <dc:creator>CHELS</dc:creator>
      <dc:date>2020-02-11T20:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624009#M183745</link>
      <description>&lt;P&gt;We don't have data set A so we can't run your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also unclear why you need to use a macro loop here rather than a data step loop (and in my mind unnecessary to use macros at all).&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2020 20:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624009#M183745</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-11T20:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624016#M183748</link>
      <description>&lt;P&gt;Agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;this is not really a macro problem.&amp;nbsp; But that said, your current code works when I run it on sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would check the case of your real data, as tranwrd is case-sensitive when searching for words to replace.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's your code, with just one value of data hard-coded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro PCase (Dat=, Var=, LowList=);

Data Cased;
x1="Long sentence with Or For And And/Or" ;

%let VarCnt=%sysfunc(countw(&amp;amp;LowList));
%do i = 1 %to &amp;amp;VarCnt;
%let Var_Ind = %scan(&amp;amp;LowList,&amp;amp;i," ");
X1 = tranwrd(X1,"&amp;amp;Var_Ind", lowcase("&amp;amp;Var_Ind"));
%end;

put x1= ;
run;
%mend PCase;

%let Low = %str(Or For And And/Or );
%PCase (Dat=A, Var=Original, LowList=&amp;amp;Low)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Returns the desired:&lt;/P&gt;
&lt;PRE&gt;x1=Long sentence with or for and and/or
NOTE: The data set WORK.CASED has 1 observations and 1 variables.
&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Feb 2020 20:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624016#M183748</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-02-11T20:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624026#M183754</link>
      <description>&lt;P&gt;Thank you for your response. The reason why I am using a macro is because there will be more data steps added inside the macro to modify the case of the variable. For example, keeping acronyms in capital letter etc.&amp;nbsp; Here is an example of my Original data with the variable named X1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;Data&lt;/FONT&gt; Original:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;input&lt;/FONT&gt; X1 $60;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;cards;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;SURGERY FOR BLEEDING&lt;/P&gt;
&lt;P&gt;DELAYED AND/OR MISSED EXAMINATIONS&lt;/P&gt;
&lt;P&gt;REMOVAL OF STITCHES&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#333399"&gt;%Macro&lt;/FONT&gt; PCase (Dat=, Var=, LowList=);&lt;/P&gt;
&lt;P&gt;Data Cased;&lt;BR /&gt;set &amp;amp;Dat;&lt;BR /&gt;X2 = propcase (&amp;amp;Var);&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;%let&lt;/FONT&gt; VarCnt=&lt;FONT color="#0000FF"&gt;%sysfunc&lt;/FONT&gt;(countw(&amp;amp;LowList));&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%do&lt;/FONT&gt; i = &lt;FONT color="#008000"&gt;1&lt;/FONT&gt; &lt;FONT color="#0000FF"&gt;%to&lt;/FONT&gt; &amp;amp;VarCnt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%let&lt;/FONT&gt; Var_Ind = &lt;FONT color="#0000FF"&gt;%scan&lt;/FONT&gt;(&amp;amp;LowList,&amp;amp;i," ");&lt;BR /&gt;X3 = tranwrd(X2, &lt;FONT color="#993366"&gt;"&amp;amp;Var_Ind"&lt;/FONT&gt;, lowcase(&lt;FONT color="#993366"&gt;"&amp;amp;Var_Ind"&lt;/FONT&gt;));&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%end;&lt;/FONT&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;FONT color="#333399"&gt;%mend&lt;/FONT&gt; PCase;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works If I define the list as: &lt;FONT color="#0000FF"&gt;%le&lt;/FONT&gt;t Low = &lt;FONT color="#0000FF"&gt;%str&lt;/FONT&gt;(Or),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color="#0000FF"&gt; %PCase&lt;/FONT&gt; (Dat=Original, Var=X1, LowList=&amp;amp;Low);&lt;/P&gt;
&lt;P&gt;it which change all the 'Or' to 'or'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I define the list as&amp;nbsp; &lt;FONT color="#0000FF"&gt;%let&lt;/FONT&gt; Low = &lt;FONT color="#0000FF"&gt;%str&lt;/FONT&gt;(Or For And/Or) then nothing works.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2020 21:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624026#M183754</guid>
      <dc:creator>CHELS</dc:creator>
      <dc:date>2020-02-11T21:21:12Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624029#M183756</link>
      <description>&lt;P&gt;I still don't see the need for a macro, when all computations occur within a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But since you provide an example ... you have upper case data, and you are searching for mixed case, and no matches are found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Feb 2020 21:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624029#M183756</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-11T21:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624074#M183770</link>
      <description>&lt;P&gt;It's normally best to make everything fully working without macro code. Here how this could look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Original;
  input X1 $60.;
  cards;
SURGERY FOR BLEEDING
DELAYED AND/OR MISSED EXAMINATIONS
REMOVAL OF STITCHES
;

data makeItWork;
  set original;
  x2=propcase(x1);
  if 0 then x3=x2;
  x3=prxchange('s/\b(and|or|for|of)\b/\l\1/oi',-1,trim(x2));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that it's working make it dynamic using a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;
%macro pcase (ds=, outds=, var=, outvar=, lowlist=);
  %if &amp;amp;outds= %then %let outds=&amp;amp;ds;
  %if &amp;amp;outvar= %then %let outvar=&amp;amp;var;
  data &amp;amp;outds;
    set &amp;amp;ds;
    if 0 then &amp;amp;outvar=&amp;amp;var;
    &amp;amp;outvar=propcase(&amp;amp;var);
    &amp;amp;outvar=prxchange("s/\b(&amp;amp;lowlist)\b/\l\1/oi",-1,trim(&amp;amp;outvar));
  run;
%mend pcase;

%let low = %str(and|or|for|of );
%pcase (ds=Original, var=x1, lowlist=&amp;amp;low);

proc print data=Original;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Below line is just to assign the new variable with the same length than an already existing variable (function prxchange() would create the new variable with a default length of 200).&lt;/P&gt;
&lt;PRE&gt;if 0 then x3=x2;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 224px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36051iB080FC902AB8A971/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And once you've got a working macro you can make it even more dynamic like allowing for changing multiple variables at once. And like always: You kind of need to keep the balance as the more you add the more complicated the code becomes. Below macro would already need quite a bit of inline comment so that you (or someone else) still can understand it it the future.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Original;
  input X1 $60.;
  x2=x1;
  cards;
SURGERY FOR BLEEDING
DELAYED AND/OR MISSED EXAMINATIONS
REMOVAL OF STITCHES
;

%macro pcase (ds=, outds=, var=, outvar=, lowlist=);
  %if &amp;amp;outds= %then %let outds=&amp;amp;ds;
  %if &amp;amp;outvar= %then %let outvar=&amp;amp;var;
  data &amp;amp;outds;
    set &amp;amp;ds;
    %do i=1 %to %sysfunc(countw(&amp;amp;outvar));
      if 0 then %scan(&amp;amp;outvar,&amp;amp;i)=%scan(&amp;amp;var,&amp;amp;i);
      %scan(&amp;amp;outvar,&amp;amp;i)=propcase(%scan(&amp;amp;var,&amp;amp;i));
      %scan(&amp;amp;outvar,&amp;amp;i)=prxchange("s/\b(&amp;amp;lowlist)\b/\l\1/oi",-1,trim(%scan(&amp;amp;outvar,&amp;amp;i)));
    %end;
  run;
%mend pcase;

%let low = %str(and|or|for|of );
%pcase (ds=Original, var=x1 x2, outvar=newX1 newX2, lowlist=&amp;amp;low);

proc print data=Original;
run;&lt;/CODE&gt;&lt;/PRE&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>Wed, 12 Feb 2020 04:02:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624074#M183770</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-12T04:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624160#M183813</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2020 13:11:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624160#M183813</guid>
      <dc:creator>CHELS</dc:creator>
      <dc:date>2020-02-12T13:11:07Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624161#M183814</link>
      <description>&lt;P&gt;Thank you so much! I really appreciate you taking the time to code and teach me!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2020 13:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624161#M183814</guid>
      <dc:creator>CHELS</dc:creator>
      <dc:date>2020-02-12T13:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: %do and %scan in maco</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624162#M183815</link>
      <description>Thank you!</description>
      <pubDate>Wed, 12 Feb 2020 13:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-and-scan-in-maco/m-p/624162#M183815</guid>
      <dc:creator>CHELS</dc:creator>
      <dc:date>2020-02-12T13:12:55Z</dc:date>
    </item>
  </channel>
</rss>

