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

Hi,

 

I have a column named journey. Under journey there're rows, eg: abc$def$fgu. Now I want to count how many "$" in each record. In this case there will be three.  Any help is appreciated! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@yichentian226 wrote:

Hi,

 

I have a column named journey. Under journey there're rows, eg: abc$def$fgu. Now I want to count how many "$" in each record. In this case there will be three.  Any help is appreciated! 

 

 


The SAS Countc function counts number of occurrences of a character in a variable:

data example;
   journey="abc$def$fgu";
   dollarcount = countc(journey,"$");
run;

Your example only shows 2 $ signs. eg: abc$def$fgu. 

If you want to count the number of things SEPARATED by the $

data example2;
   journey="abc$def$fgu";
   dollarcount = countw(journey,"$");
run;

Countw counts "words". The second parameter would be a list of characters used to separate the pieces of the value. This just uses the $ sign to separate.

View solution in original post

2 REPLIES 2
ballardw
Super User

@yichentian226 wrote:

Hi,

 

I have a column named journey. Under journey there're rows, eg: abc$def$fgu. Now I want to count how many "$" in each record. In this case there will be three.  Any help is appreciated! 

 

 


The SAS Countc function counts number of occurrences of a character in a variable:

data example;
   journey="abc$def$fgu";
   dollarcount = countc(journey,"$");
run;

Your example only shows 2 $ signs. eg: abc$def$fgu. 

If you want to count the number of things SEPARATED by the $

data example2;
   journey="abc$def$fgu";
   dollarcount = countw(journey,"$");
run;

Countw counts "words". The second parameter would be a list of characters used to separate the pieces of the value. This just uses the $ sign to separate.

yichentian226
Obsidian | Level 7
THANKS SO MUCH! You rock!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 702 views
  • 0 likes
  • 2 in conversation