BookmarkSubscribeRSS Feed
Vasundha
Calcite | Level 5
Hi Everyone,
A newbie needs your help. May I know how to find the count of a specific delimiter in a string variable column.
For instance,
String = gfhabciklm+7/refghibklr+8/abcdefghij+9;
I need the count of " +" only In this string irrespective of other delimiter.
Looking forward to getting some support from you guys. Thankyou.
27 REPLIES 27
Patrick
Opal | Level 21

@Vasundha Welcome to the SAS Communities.

Here you go:

data demo;
 have='String = gfhabciklm+7/refghibklr+8/abcdefghij+9;';
 count_plus=countc(have,'+');
run;

proc print data=demo;
run;

Patrick_0-1654685120389.png

 

And you didn't ask specifically but just because you were mentioning the term "delimiter" here how you could read such a string into SAS variables using the plus sign as delimiter.

data demo2;
  infile datalines truncover dsd dlm='+';
  input (var1-var5) (:$40.);
  datalines4;
gfhabciklm+7/refghibklr+8/abcdefghij+9;
;;;;
proc print data=demo2;
run;

Patrick_0-1654685394033.png

 

 

Vasundha
Calcite | Level 5
Hi Patrick, thankyou for providing the result but, i used the same concept. I've got for some fields the count is blank even though it has a (+) symbol in the string value. And more over I've a long value in the same column which has 7 +'s but when I used your concept it is showing only the count is 1.
I created such values in a column in excel file and importing that file to sas.
Patrick
Opal | Level 21

I'm rather certain that the code I've provided will work as desired. If it's not working for you then eventually your source string is truncated.

Ideally provide some sample data or even the Excel as an attachment (many people won't open this ...but I will) where things are not working for you. It's kind-of impossible to address your problem without seeing your code and being able to replicate what you describe.

Vasundha
Calcite | Level 5
Yes, you're so right. My data has been truncated and i figured out that some values has ++'s so, I added additional variable for ++'s in your code but, despite of picking the ++'s count, it is picking single +'s count only for both the count variables. Could you help me on that to achieve this. Thankyou.
Patrick
Opal | Level 21

Sure. So what exactly do you need? Count consecutive ++ only once or define a delimiter that deals with repeated ++.

If it's the delimiter bit: Would a repeated ++ be a single delimiter or indicate there the is just no value between the plusses?

 

Please provide a sample string and then show the desired result.

Vasundha
Calcite | Level 5

Sure here it is, 

String_variable
Abcdefghi+7/edfiklhopr+9
Jakdkopiqwe++9/ajsjsjajsj++10/jajsjakam++4/hefghikqp++1

I need two count variables for + and ++

Output:

Count_plus1   count_plus2

2                              4

And if there's no ++ the count_plus2 should be 0.

 

Patrick
Opal | Level 21

If you can be certain that you ever only get one or two consecutive plusses then below code should work. 

If the number of consecutive plusses can vary and is "undefined" then things will become quickly quite a bit more involved and require more advanced coding.

data have;
  infile datalines truncover;
  input string :$60.;
  datalines4;
Abcdefghi+7/edfiklhopr+9
Jakdkopiqwe++9/ajsjsjajsj++10/jajsjakam++4/hefghikqp++1
;;;;
data want;
  set have;
  count_all=countc(string,'+','it');
  count_2plus=count(string,'++','it');
  count_1plus=count_all-(count_2plus*2);
  drop count_all;
run;
proc print data=want;
run;

Patrick_0-1654689393135.png

 

Vasundha
Calcite | Level 5
Thank you. I've got blank values for count_1plus.
Patrick
Opal | Level 21

@Vasundha wrote:
Thank you. I've got blank values for count_1plus.

Not sure what you mean by that. 

The "best" and quickest way to get what you need: Provide to us sample data in the form of a working data have step same as I used in my last post. Try for this data have step to create representative sample data covering all the real cases you're dealing with. Then show us based on the sample data the desired outcome and describe the logic required to get to this outcome.
....and often people also appreciate if you share the code you've already tried to make this work. Even if such code doesn't work yet, it does help to understand your current thought process as well as your current level of SAS coding skills so people can provide answers suitable to your level.

Vasundha
Calcite | Level 5
Please make a bit change in the concept. Include 0 in count_2plus only if there's no ++, not for count_1plus.
Patrick
Opal | Level 21

Please read my last post. You need to be very clear and best provide representative sample data and desired result.

It's already late in my part of the world so signing off - but I'm sure someone else will pick-up from here.

Vasundha
Calcite | Level 5
Alright. Thank you very much.
Vasundha
Calcite | Level 5

Replying to All and posting it again.

String_variable
Abcdefghi+7/edfiklhopr+9
Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1

Required Output:
Count_1plus count_2plus
2 0
0 4

Patrick's code working nicely but it is giving me the output like this and considering ++ as 2 single ++ instead of considering only 1 +. Please help me on achieving the required output which I mentioned above. Thankyou.
The Output I got :
Count_1plus  count_2plus
2                0
4               4
Tom
Super User Tom
Super User

Post data as a data step.

data have ;
  input String_variable $80.;
cards;
Abcdefghi+7/edfiklhopr+9
Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1
;

Post code.

data want;
  set have;
  count1 = countc(string,'+');
  count2 = count(string,'++');
run;

Show results:

Obs    string                                                     count1    count2

 1     Abcdefghi+7/edfiklhopr+9                                      2         0
 2     Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1       8         4

If the results are not what you want then explain what you want.

For example you seem to think there are ZERO plus signs in that second string, but as the results show there are clearly 8 plus signs there.  Why did you think there should be zero?

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
  • 27 replies
  • 1533 views
  • 0 likes
  • 3 in conversation