@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;
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;
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.
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.
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.
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;
@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.
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.
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
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?
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!
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.