BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CWilson4815
Fluorite | Level 6

Hi guys,

 

I've got a table of data where a certain string of text can appear in a variety of variables.

 

I want to use one computed column (in query builder) that counts how many times the string appears in a range of variables, rather than using multiple computed columns to count the occurrence of the string in each variable separately, as you would with the COUNTIF function over a range of cells in Excel.

 

If it helps, I'm using SAS EG 7.1.

 

Thanks in advance,

Chris

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use the {i} above post for the code window to preserve formatting.  The colon indicates all variables with the prefix of the text before it, so you can read as "of all variables with decisiononothercoe prefix":

count(catx(',',of DECISIONOTHERCODES:),'RS006 - CONSUMER.PRIMARY')) AS RS006

  That syntax should work - I have none of your data to work with of course, but a similar code does:

proc sql;
  select count(catx(',',make,model,type),"Acura") as tmp
  from sashelp.cars;
quit;

View solution in original post

6 REPLIES 6
CWilson4815
Fluorite | Level 6
Thanks for the reply RW9 but how can I use this function with multiple variables being fed into the formula?



For example, if I separate them by commas, then the function doesn't work.



To put it into context, I have 10 columns I would like to reference (DECISIONOTHERCODE1,...,DECISIONOTHERCODE10) and my table name is t1.


RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is why we always ask for test data, its impossible to guess what you are working with.  You can concatenate the wanted variables together then count across that:

count(catx(',',of decisiononothercode:),'myword') as want

So the catx() takes all variables with prefix decisiononothercode, puts all the strings together with commas separating.  Then the count function scans that string for occurences of myword and returns the number found.

CWilson4815
Fluorite | Level 6

I understand the logic behind these functions but I am receiving a syntax error when trying to run this in my query builder as below:

 

(COUNT(CATX(',',of DECISIONOTHERCODE:),'RS006 - CONSUMER.PRIMARY')) AS RS006
_________________
22
202
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, ), *, **, +, ',', -, '.', /, <, <=, <>, =, >, >=, ?, AND,
BETWEEN, CONTAINS, EQ, EQT, GE, GET, GT, GTT, IN, IS, LE, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, ^, ^=, |, ||,
~, ~=.

ERROR 202-322: The option or parameter is not recognized and will be ignored.

 

What exactly does the of and colon do in your code?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use the {i} above post for the code window to preserve formatting.  The colon indicates all variables with the prefix of the text before it, so you can read as "of all variables with decisiononothercoe prefix":

count(catx(',',of DECISIONOTHERCODES:),'RS006 - CONSUMER.PRIMARY')) AS RS006

  That syntax should work - I have none of your data to work with of course, but a similar code does:

proc sql;
  select count(catx(',',make,model,type),"Acura") as tmp
  from sashelp.cars;
quit;
CWilson4815
Fluorite | Level 6

I followed your code from the cars example to help as the of and : function still wasn't working for me.

 

So I ended up having to write it as follows:

 

COUNT(CATX(',',DECISIONOTHERCODE1,DECISIONOTHERCODE2,DECISIONOTHERCODE3,DECISIONOTHERCODE4,DECISIONOTHERCODE5,DECISIONOTHERCODE6,DECISIONOTHERCODE7,DECISIONOTHERCODE8,DECISIONOTHERCODE9,DECISIONOTHERCODE10),'RS006 - CONSUMER.PRIMARY')

Thanks a lot for your help.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 18835 views
  • 2 likes
  • 2 in conversation