DATA Step, Macro, Functions and more

Count function

Reply
Occasional Contributor
Posts: 9

Count function

Hi all,

I have the following problem. I have a string variable which i want to search for a list of specific expressions with the help of the count function. My expressions contain sometimes "," or "(" or ")" or other signs which result in an error for this function. I checked the documentation for the count function, however it sounds like greek to me Smiley Happy
How do I have to specify the function in order to work properly with these other signs?

Thanks,

Valentin
Super Contributor
Super Contributor
Posts: 365

Re: Count function

Posted in reply to Valentin_HU
Hello Valentin,

This is an example of using COUNT:
[pre]
data i;
s=",,,,)))((";
comma=COUNT(s,",");
close=COUNT(s,")");
open=COUNT(s,"(");
run;
[/pre]
Sincerely,
SPR
Occasional Contributor
Posts: 9

Re: Count function

HI SPR,

thanks for your answer. However, I think I didn't make my question clear enqough. Sorry for that! I'm searching in a variable for a list of expressions (the list is a macro variable that contains expressions which are delimited by a "*" and each word is searched with the help of a loop), and some of these expressions contain closed or open brackets as well as commas (e.g., "have, in the past,"). When I search for these expressions in a macro (text is the variable I am searching) with
count_word=count(text,&expressions);
then SAS gives back an error: "More positional parameters found than defined."

Here is my complete code:

%macro count_exp_gen(list);
%let count=1;
%let delim=*;
%let express=%scan(&list,&count,&delim);
%do %while (%length(&express) ne 0);

count_gen_&count = countc(text,"&express",'w') ;

%let count=%eval(&count + 1);
%let express=%scan(&list,&count,&delim);
%end;
%mend;

The macro variable is given by:

GLOBAL GENERALS
INTHEFUTURE*FORSPECULAT*FORTRAD*HOWANDWHY*SPECULATIVETRAD*ACCOUNTFOR*MAY*HEDGINGOFOURCOMMONSTOCK*APPRO
PRIATE*WOULDREQUIREDISCLOSURE*

Hope this helps to understand my question.

Thanks,

Valentin
Valued Guide
Posts: 2,177

Re: Count function

Posted in reply to Valentin_HU
sas macro processing is designed to mingle program instructions with data,
and you are expected to provide control when there might be doubt like whether [pre]("have, in the past,"). [/pre] provides program or data.
The macro quoting fuinctions are intended to provide this control.
If your data in the list in the macro variable are reliable, then %str(&list) might
provide enough protection, otherwise use stronger quoting, like:
%superQ(list) and %Qscan(%superQ(list), &count, &delim)
Super Contributor
Super Contributor
Posts: 365

Re: Count function

Posted in reply to Valentin_HU
I made some changes in the code. Now it works but what is the output? count?
[pre]
%macro count_exp_gen(list);
%let count=1;
%let delim=*;
%let express=%scan(&list,&count,&delim);
%do %while (%length(&express) ne 0);
%let count_gen_&count = countc(&list,"&express",'w') ;
%let count=%eval(&count + 1);
%let express=%scan(&list,&count,&delim);
%end;
%mend;
%let generals=INTHEFUTURE*FORSPECULAT*FORTRAD*HOWANDWHY*SPECULATIVETRAD*ACCOUNTFOR*MAY*HEDGINGOFOURCOMMONSTOCK*APPROPRIATE*WOULDREQUIREDISCLOSURE*;
%count_exp_gen(&generals);
[/pre]
SPR
Occasional Contributor
Posts: 9

Re: Count function

Posted in reply to Valentin_HU
First of all, thanks a lot for your answers.

I will try once again to make clear what my problem is.

I have a dataset that includes a variable with the name "text" and I have a list of expressions (this list is given by the macro variable "generals"). Now the task I want to perform is to generate one variable in my initial dataset for each expression in "generals" that gives back the number of times that expression appears in the variable "text".
This works as long as there are no commas or brackets in the expression list.

I will try to give you a simplified version of my full code:

data temp;
length text $ 50;
input Text $;
datalines;
text1,text2,(text3)
run;

%let expressions=text1*text2,*(text3)*;

%macro count_exp_gen(list);
%let count=1;
%let delim=*;
%let express=%scan(&list,&count,&delim);
%do %while (%length(&express) ne 0);

count_gen_&count = count(text,"&express") ;

%let count=%eval(&count + 1);
%let express=%scan(&list,&count,&delim);
%end;
%mend;

data temp2;
set work.temp;
%count_exp_gen(&expressions);
run;
Super Contributor
Super Contributor
Posts: 365

Re: Count function

Posted in reply to Valentin_HU
Hello Valentin,

I do not see a problem with brackets but commas are a problem for sure because if macro variable EXPRESSIONS contains commas then function %SCAN in this statement

%let express=%scan(&list,&count,&delim);

gets additional unexpected arguments.

I think that as remedy one can replace commas with underscores, for example, like this
[pre]
data temp;
length text $ 50;
input Text $;
text=TRANWRD(text,",","_");
datalines;
text1,text2,(text3)
run;

%let expressions=text1_*text2_*(text3)*;
[/pre]
Sincerely,
SPR Message was edited by: SPR
Occasional Contributor
Posts: 9

Re: Count function

Ah ok! Thank you, I will implement your solution!
Super User
Posts: 10,020

Re: Count function

Posted in reply to Valentin_HU
I think you need automatic macro variable 'parmbuff'
Such as : %macro test /parmbuff; ...... &syspbuff....
Or using call execute() which is macro interface with dataset.


Ksharp
Ask a Question
Discussion stats
  • 8 replies
  • 296 views
  • 0 likes
  • 4 in conversation