BookmarkSubscribeRSS Feed
Valentin_HU
Calcite | Level 5
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 🙂
How do I have to specify the function in order to work properly with these other signs?

Thanks,

Valentin
8 REPLIES 8
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Valentin_HU
Calcite | Level 5
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
Peter_C
Rhodochrosite | Level 12
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)
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Valentin_HU
Calcite | Level 5
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;
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Valentin_HU
Calcite | Level 5
Ah ok! Thank you, I will implement your solution!
Ksharp
Super User
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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1754 views
  • 0 likes
  • 4 in conversation