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

Count the number of times the string ‘ ? ‘ occurs in variable line. Choose any method that works reliably. Regardless of method, assume that a line won’t consist entirely of ‘ ? ‘’s.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Easy enough to test a function. Find the required parameters from the documentation of function and then use it. A vast majority of the functions in the SAS documentation have at least one example.

 

From Countc:

data test;
   string  = 'Baboons Eat Bananas     ';
   a       = countc(string, 'a');
   b       = countc(string, 'b');
   b_i     = countc(string, 'b', 'i');
   abc_i   = countc(string, 'abc', 'i');
      /* Scan string for characters that are not "a", "b", */
      /* and "c", ignore case, (and include blanks).       */
   abc_iv  = countc(string, 'abc', 'iv');
      /* Scan string for characters that are not "a", "b", */
      /* and "c", ignore case, and trim trailing blanks.   */
   abc_ivt = countc(string, 'abc', 'ivt');
run;

Key part are the source, the variable String above, and the character list to search for, the second parameter. The 3rd optional parameter has to do with ignoring letter case, adding or excluding certain types of characters that are hard to type and keep in code such as Tab or control characters (ancient stuff from teletype but include things like the "ring bell", vertical space without end of line, and stuff)

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

So what strategies, in words, do you think might work?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
klj4y
Calcite | Level 5

I would think the countc function. We were not provided data to test it on so i'm not sure how it would work.

ballardw
Super User

Easy enough to test a function. Find the required parameters from the documentation of function and then use it. A vast majority of the functions in the SAS documentation have at least one example.

 

From Countc:

data test;
   string  = 'Baboons Eat Bananas     ';
   a       = countc(string, 'a');
   b       = countc(string, 'b');
   b_i     = countc(string, 'b', 'i');
   abc_i   = countc(string, 'abc', 'i');
      /* Scan string for characters that are not "a", "b", */
      /* and "c", ignore case, (and include blanks).       */
   abc_iv  = countc(string, 'abc', 'iv');
      /* Scan string for characters that are not "a", "b", */
      /* and "c", ignore case, and trim trailing blanks.   */
   abc_ivt = countc(string, 'abc', 'ivt');
run;

Key part are the source, the variable String above, and the character list to search for, the second parameter. The 3rd optional parameter has to do with ignoring letter case, adding or excluding certain types of characters that are hard to type and keep in code such as Tab or control characters (ancient stuff from teletype but include things like the "ring bell", vertical space without end of line, and stuff)

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1230 views
  • 1 like
  • 4 in conversation