SAS Enterprise Guide

Desktop productivity for business analysts and programmers
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 6 replies
  • 20234 views
  • 2 likes
  • 2 in conversation