BookmarkSubscribeRSS Feed
Naveen1111
Calcite | Level 5

Hi, 

i have used the talent dataset. Could someone tell me why there is a difference between x and y results?

 

data tp;
set sasuser.talent;
blank=strip(Agency);
x=countc(blank," ");
y=countc(strip(Agency)," ");

keep blank x y Agency;

run;

 

 

 

results

 

Obs Agency blank x y

1 TALENT TALENT TALENT TALENT TALENT TALENT 2 2
2 GEORGE STICKS GEORGE STICKS 8 1
3 UNIVERSAL UNIVERSAL 11 0
4 HORIZONS HORIZONS 12 0
5 SYCAMORE SYCAMORE 12 0
6 GOLDEN GUILD GOLDEN GUILD 9 1
7 GOLDEN GUILD GOLDEN GUILD 9 1
8 ACTORS SQUARE ACTORS SQUARE 8 1
9 LAURELS LAURELS 13 0
10 ARTS TALENT ARTS TALENT 10 1
11 FINDERS FINDERS 13 0
12 ALL STARS ALL STARS 12 1
13 ALL STARS ALL STARS 12 1
14 ALL STARS ALL STARS 12 1
15 UNLIMITED UNLIMITED 11 0
16 STARS AND STRIPES STARS AND STRIPES 5 2
17 SETTINGS SETTINGS 12 0
18 STARS AND STRIPES STARS AND STRIPES 5 2
19 UNIVERSAL LIGHTS UNIVERSAL LIGHTS 5 1
20 MODELS AND MAGIC MODELS AND MAGIC 6 2
21 HORIZONS HORIZONS 12 0
22 HARDY STELSON HARDY STELSON 8 1
23 AKA TALENT AKA TALENT 11 1
24 MODELS AND MAGIC MODELS AND MAGIC 6 2

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Because you have leading and/or trailing blanks in your input variable. The Strip Function removes them

 

data test;
   a  = ' abc  ';
   ca = countc(a, ' ');
   cb = countc(strip(a), ' ');
run;

 

Naveen1111
Calcite | Level 5

indeed, so on that basis, results should be identical as in blank variable i used strip function, which removed the leading and trailing blanks. But when i used x variable (x=countc(blank," ");)with countc function, still showing spaces, not in y variable case(y=countc(strip(Agency)," ");)

 

 

PeterClemmensen
Tourmaline | Level 20

I'm not sure what you mean by this?

ballardw
Super User

@Naveen1111 wrote:

indeed, so on that basis, results should be identical as in blank variable i used strip function, which removed the leading and trailing blanks. But when i used x variable (x=countc(blank," ");)with countc function, still showing spaces, not in y variable case(y=countc(strip(Agency)," ");)

 

 


The variable Blank, when created is the same length as agency. As such it will be padded at the end with blanks.

Example below shows the result of a "stripped" variable sandwiched between : characters. Countc counts them unless explicitly stripped at the time of counting.

data example;
   x='   Some string with lead and trail blanks  ';
   blank=strip(x);
   string= ':'||blank||':';
   y=countc(string,"");
   put string=;
run;

Look in the log to see value of string.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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