BookmarkSubscribeRSS Feed
Vasundha
Calcite | Level 5
Required results:

Obs    string                                                                                        count1    count2

 1     Abcdefghi+7/edfiklhopr+9                                                                         2         0
 2     Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1                                          0         4
Vasundha
Calcite | Level 5
It's because of the plus is repeating twice not just once. That's why I asked zero. Please have a look at the required output which I replied to you. Thankyou.
Tom
Super User Tom
Super User

@Vasundha wrote:
It's because of the plus is repeating twice not just once. That's why I asked zero. Please have a look at the required output which I replied to you. Thankyou.

So you only what to count plus signs that are not next to other plus signs?

You could perhaps try using regular expressions to do something that complex.

But in this limited case just get the double plus signs out of the way before counting.

countc(tranwrd(string,'++',' '),'+')

 

PS If you are trying to parse expressions in some language like C where + and ++ are different operators then SAS is probably not the language to use. There are languages that are designed for building parsers and compilers.

Vasundha
Calcite | Level 5

My point is if the string value has 1 plus sign followed by slash / then consider count1 as 1. If the string value has 2 plus sign followed by slash / then consider count1 as 1. Otherwise 0's.

Consider this output:

Required result:

Obs    string                                                    count1    count2

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

 

Tom
Super User Tom
Super User

@Vasundha wrote:

My point is if the string value has 1 plus sign followed by slash / then consider count1 as 1. If the string value has 2 plus sign followed by slash / then consider count1 as 1. Otherwise 0's.

Consider this output:

Required result:

Obs    string                                                    count1    count2

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

 


When did / become part of the problem?  What do the / characters mean?

Why is the first count 2 instead of 1?  The final + does not have a / after it.

Why is that last count 4 and not 3?  The final ++ does not have a / after it.

Vasundha
Calcite | Level 5
I've been saying this in a better way by considering my required output, you will understand it more clearly. In that long string values 1st string value has only single +'s so count the no. Of single pluses and put the count as 2 in count1 variable.
In the 2nd long string value has double ++'s so count the no.of double pulses and put the count as 4 in count1 variable. Please please try to understand my concept with my Required Output. Here slash / becomes the end of one or double pluses and at the end of the string value if the string doesn't end with slash / no worries, you can consider the + or ++. Slash / is for our understanding.
Tom
Super User Tom
Super User

Perhaps you have misrepresented the actual problem?

Are you string really delimited by / and not + or ++ ?  Is what you are trying to count is how many substrings contain a + character?

data have ;
  input string $80.;
cards;
Abcdefghi+7/edfiklhopr+9
Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1
George
Sam/Fred/x+y
;
data want;
  set have;
  count=0;
  do words=1 to countw(string,'/');
      if indexc(scan(string,words,'/'),'+') then count=count+1;
  end;
  words=words-1;
run;
Obs    string                                                     count    words

 1     Abcdefghi+7/edfiklhopr+9                                     2        2
 2     Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1      4        4
 3     George                                                       0        1
 4     Sam/Fred/x+y                                                 1        3

Vasundha
Calcite | Level 5
Nope. You've completely misunderstood the concept. The delimiter count I want is only for +'s and ++'s not slash. In my all previous posts I've not said anything about consider slash / as the delimiter to count it.
Tom
Super User Tom
Super User

@Vasundha wrote:
I've been saying this in a better way by considering my required output, you will understand it more clearly. In that long string values 1st string value has only single +'s so count the no. Of single pluses and put the count as 2 in count1 variable.
In the 2nd long string value has double ++'s so count the no.of double pulses and put the count as 4 in count1 variable. Please please try to understand my concept with my Required Output. Here slash / becomes the end of one or double pluses and at the end of the string value if the string doesn't end with slash / no worries, you can consider the + or ++. Slash / is for our understanding.

Here is what you just described:

data want;
  set have;
  if index(string,'++') then count=count(string,'++');
  else count=count(string,'+');
run;

Result

Obs    string                                                     count

 1     Abcdefghi+7/edfiklhopr+9                                     2
 2     Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1      4
 3     George                                                       0
 4     Sam/Fred/x+y                                                 1

So what is the count for something like:

 

a+b++c

 

Vasundha
Calcite | Level 5
Need seperate variables for + and ++ i.e count1 and count2. Thankyou.
Tom
Super User Tom
Super User

@Vasundha wrote:
Need seperate variables for + and ++ i.e count1 and count2. Thankyou.

We figured that out already.

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

 1     Abcdefghi+7/edfiklhopr+9                                      2         0
 2     Jakdkopiqwe++9/ajsjsjajsj++10/ghakakak++12/hwjwjwnnw++1       0         4
 3     George                                                        0         0
 4     Sam/Fred/x+y                                                  1         0



Vasundha
Calcite | Level 5

a+b++c the count is

Count1 for +    count2 for ++

1                              1

Vasundha
Calcite | Level 5
It's not a operator, just a character in my string value.

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
  • 1550 views
  • 0 likes
  • 3 in conversation