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

Hello!

 

I have a quick question I hope you can answer:

 

There is a string "The red car jumped over the bridge."  How do I write a statement that counts the number of blanks in a string?

 

Thank you so much!

1 ACCEPTED SOLUTION

Accepted Solutions
light
Obsidian | Level 7

Hi Tom,

 

Thank you greatly for variaty of solutions 🙂

 

The third one is my favorate, because it can count leading and trailing blanks too!

 

Best Wishes 🙂

 

 

View solution in original post

7 REPLIES 7
light
Obsidian | Level 7

I will try this!  Thank you Wong!

Astounding
PROC Star

n_blanks = length(var) - length(compress(var));

light
Obsidian | Level 7

Hi Astounding,

 

Works great for leading blanks and blanks between words!  I am also trying to count trailing blanks in a string too, if possible. 

 

 

Thank you so much 🙂

 

 

Tom
Super User Tom
Super User

In the past I always used 

 

length(string)-length(compress(string,' '))

But now with new functions like COUNTC() you could also use

 

 

countc(trim(string),' ')

You need to be careful when counting spaces in variables because SAS stores all character variables as fixed length strings that are padded with spaces.  So if you did this:

data _null_
  c1=countc("The red car jumped over the bridge.",' ');
  put c1=;
run;

You would get 6, but if you did this:

data _null_;
  length x $50;
  x="The red car jumped over the bridge.";
  c1=countc(x,' ');
  put c1=;
run;

You would get 21.

 

light
Obsidian | Level 7

Hi Tom,

 

Thank you greatly for variaty of solutions 🙂

 

The third one is my favorate, because it can count leading and trailing blanks too!

 

Best Wishes 🙂

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

For future reference, please mark the post of the person who correctly answered your question as the correct answer, not a reply to it that you have posted.  This helps keep acurate answers below the questions, and gives some credit to the poster who answered the question. Also noted on the post "Sum digits of a string".

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 14456 views
  • 8 likes
  • 5 in conversation