DATA Step, Macro, Functions and more

Why the do loop is not working

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Why the do loop is not working

[ Edited ]

DATA COMMENT;

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

;

 

 

options mprint mlogic symbolgen;

%macro word_count(input=,var=,output=,order=);

data temp;

set &input.;

%do i = 1 %to %sysfunc(countw(&var.));

var1 = scan(&var.,&i.);

output;

%end;

run;

 

proc sql;

create table &output. as select var1, count(*) as count from temp group by var1 order by count &order.;

quit;

 

%mend word_count;

 

%word_count(input=comment,output=new,var=comment,order=desc);

 

LOg Message :

MLOGIC(WORD_COUNT): %DO loop beginning; index variable I; start value is 1; stop value is 1; by

value is 1.


Accepted Solutions
Solution
‎05-22-2018 06:11 AM
PROC Star
Posts: 266

Re: Why the do loop is not working

Posted in reply to ruchi11dec

You problem seems to be with the COUNTW function, when &var=COMMENT, then %sysfunc(countw(&var)) is equal to countw('COMMENT'), which is one.

I think you should drop the macro loop and use a data step loop instead:

%macro word_count(input=,var=,output=,order=);

data temp;

  set &input.;

  do _N_ = 1 to countw(&var.);

    var1 = scan(&var.,_N_);

    output;

    end;

run;

 

proc sql;

  create table &output. as select var1, count(*) as count from temp group by var1 order by count &order.;

quit;

 

%mend word_count;

 

I used _N_ as a temporary variable, as you save a DROP statement that way.

View solution in original post


All Replies
Solution
‎05-22-2018 06:11 AM
PROC Star
Posts: 266

Re: Why the do loop is not working

Posted in reply to ruchi11dec

You problem seems to be with the COUNTW function, when &var=COMMENT, then %sysfunc(countw(&var)) is equal to countw('COMMENT'), which is one.

I think you should drop the macro loop and use a data step loop instead:

%macro word_count(input=,var=,output=,order=);

data temp;

  set &input.;

  do _N_ = 1 to countw(&var.);

    var1 = scan(&var.,_N_);

    output;

    end;

run;

 

proc sql;

  create table &output. as select var1, count(*) as count from temp group by var1 order by count &order.;

quit;

 

%mend word_count;

 

I used _N_ as a temporary variable, as you save a DROP statement that way.

Occasional Contributor
Posts: 17

Re: Why the do loop is not working

Hey thanks for the reply..
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

Super User
Super User
Posts: 9,599

Re: Why the do loop is not working

Posted in reply to ruchi11dec

What exactly is it you are trying to do with this code?  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.  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:

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;
set have;
count=countw(comment);
run;

Notice that I don't code in shouting case, and avoid using things like "new" for dataset names, keywords can cause confusion.

 

Occasional Contributor
Posts: 17

Re: Why the do loop is not working

Hey..yes I know this does not makes sense.. I just tried to simplify the whole big program I was working on... :-)

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 139 views
  • 0 likes
  • 3 in conversation