<?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: Why the do loop is not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463968#M118275</link>
    <description>&lt;P&gt;You problem seems to be with the COUNTW function, when &amp;amp;var=COMMENT, then %sysfunc(countw(&amp;amp;var)) is equal to countw('COMMENT'), which is one.&lt;/P&gt;&lt;P&gt;I think you should drop the macro loop and use a data step loop instead:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%macro word_count(input=,var=,output=,order=);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data temp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set &amp;amp;input.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do _N_ = 1 to countw(&amp;amp;var.);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; var1 = scan(&amp;amp;var.,_N_);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc sql;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; create table &amp;amp;output. as select var1, count(*) as count from temp group by var1 order by count &amp;amp;order.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%mend word_count;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used _N_ as a temporary variable, as you save a DROP statement that way.&lt;/P&gt;</description>
    <pubDate>Tue, 22 May 2018 07:19:08 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-05-22T07:19:08Z</dc:date>
    <item>
      <title>Why the do loop is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463967#M118274</link>
      <description>&lt;P&gt;DATA COMMENT;&lt;/P&gt;&lt;P&gt;INPUT ID COMMENT $ 150.;&lt;/P&gt;&lt;P&gt;INFILE DATALINES truncover;&lt;/P&gt;&lt;P&gt;DATALINES;&lt;/P&gt;&lt;P&gt;1 SERVICE IS GOOD but can improve&lt;/P&gt;&lt;P&gt;2 SERVICE IS BAD&lt;/P&gt;&lt;P&gt;3 STAFF CAN IMPROVE and also the food&lt;/P&gt;&lt;P&gt;4 happy customer&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;options mprint mlogic symbolgen;&lt;/P&gt;&lt;P&gt;%macro word_count(input=,var=,output=,order=);&lt;/P&gt;&lt;P&gt;data temp;&lt;/P&gt;&lt;P&gt;set &amp;amp;input.;&lt;/P&gt;&lt;P&gt;%do i = 1 %to %sysfunc(countw(&amp;amp;var.));&lt;/P&gt;&lt;P&gt;var1 = scan(&amp;amp;var.,&amp;amp;i.);&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table &amp;amp;output. as select var1, count(*) as count from temp group by var1 order by count &amp;amp;order.;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend word_count;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%word_count(input=comment,output=new,var=comment,order=desc);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;LOg Message :&lt;/P&gt;&lt;P&gt;MLOGIC(WORD_COUNT): %DO loop beginning; index variable I; start value is 1; stop value is 1; by&lt;/P&gt;&lt;P&gt;value is 1.&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 07:00:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463967#M118274</guid>
      <dc:creator>ruchi11dec</dc:creator>
      <dc:date>2018-05-22T07:00:14Z</dc:date>
    </item>
    <item>
      <title>Re: Why the do loop is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463968#M118275</link>
      <description>&lt;P&gt;You problem seems to be with the COUNTW function, when &amp;amp;var=COMMENT, then %sysfunc(countw(&amp;amp;var)) is equal to countw('COMMENT'), which is one.&lt;/P&gt;&lt;P&gt;I think you should drop the macro loop and use a data step loop instead:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%macro word_count(input=,var=,output=,order=);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data temp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; set &amp;amp;input.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do _N_ = 1 to countw(&amp;amp;var.);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; var1 = scan(&amp;amp;var.,_N_);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc sql;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; create table &amp;amp;output. as select var1, count(*) as count from temp group by var1 order by count &amp;amp;order.;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%mend word_count;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used _N_ as a temporary variable, as you save a DROP statement that way.&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 07:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463968#M118275</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-05-22T07:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Why the do loop is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463973#M118277</link>
      <description>&lt;P&gt;What exactly is it you are trying to do with this code?&amp;nbsp; The problem I see it is that you have fallen for the age old issue of doing macro - which is nothing more than a text find/replace, rather than get a good basis in the actual programming language which is Base SAS.&amp;nbsp; None of the code you presented actually seems to do anything meaningful, the macro and the macro loop for instance just generate a whole lot of Base SAS statements, which you can of course achieve in many ways, but simply enough by:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id comment $ 150.;
  infile datalines truncover;
datalines;
1 SERVICE IS GOOD but can improve
2 SERVICE IS BAD
3 STAFF CAN IMPROVE and also the food
4 happy customer
;
run;

data want;&lt;BR /&gt;  set have;&lt;BR /&gt;  count=countw(comment);&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;Notice that I don't code in shouting case, and avoid using things like "new" for dataset names, keywords can cause confusion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 07:59:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463973#M118277</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-22T07:59:02Z</dc:date>
    </item>
    <item>
      <title>Re: Why the do loop is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463996#M118285</link>
      <description>&lt;P&gt;Hey thanks for the reply..&lt;BR /&gt;I was not able to understand..initially I wrote the simple program and then I decided to do it in the macro way, and I was clueless why it is not working... Yes it works this way..Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 10:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463996#M118285</guid>
      <dc:creator>ruchi11dec</dc:creator>
      <dc:date>2018-05-22T10:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Why the do loop is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463997#M118286</link>
      <description>&lt;P&gt;Hey..yes I know this does not makes sense.. I just tried to simplify the whole big program I was working on... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 10:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-do-loop-is-not-working/m-p/463997#M118286</guid>
      <dc:creator>ruchi11dec</dc:creator>
      <dc:date>2018-05-22T10:11:31Z</dc:date>
    </item>
  </channel>
</rss>

