SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data work.cities;
	input city $char15.;
	datalines;
New York 
    Los Angeles
  Las Vegas  
  San   Diego
 
;
run;

How to remove leading blanks 

left function cannot accomplish this task

compress and strip both remove leading and trailling 

i want to remove only leading blanks from string

7 REPLIES 7
Ksharp
Super User
data work.cities;
	input city $char15.;
want=prxchange('s/^\s+//',1,city);
	datalines;
New York 
    Los Angeles
  Las Vegas  
  San   Diego
	xxxxxxxxx
;
run;
ballardw
Super User

Please describe exactly how "left function cannot accomplish this task".

 

And perhaps do you mean more that just leading blanks? If you mean the extra spaces in the middle of "

San   Diego

 those would not be "leading blanks".

 

You also do not understand that "trailing blanks" are only of concern at certain uses. SAS character variables that are not defined the the full length of a variable will have "trailing blanks" only for some operations. Always.

 


@BrahmanandaRao wrote:
data work.cities;
	input city $char15.;
	datalines;
New York 
    Los Angeles
  Las Vegas  
  San   Diego
 
;
run;

How to remove leading blanks 

left function cannot accomplish this task

compress and strip both remove leading and trailling 

i want to remove only leading blanks from string


 

BrahmanandaRao
Lapis Lazuli | Level 10

Left function can does left allignment only it can not any string manuplations

ballardw
Super User

@BrahmanandaRao wrote:

Left function can does left allignment only it can not any string manuplations


Suggest that you try this code and see if that matches what you say above.

data work.cities;
	input city $char15.;
   city=left(city);
	datalines;
New York 
    Los Angeles
  Las Vegas  
  San   Diego
;
run;

There are places in REPORT functions where Left is a justification and does not change the actual value. But the Function Left does just fine for removing spaces at the beginning of a string. If you have characters that don't visibly print and are not spaces, such as Tab or ASCII 255, it may appear as though the "space" was not removed because the actual character was not in fact a space.

Astounding
PROC Star

The $char informats are designed to preserve leading blanks.  If you don't want leading blanks, switch to:

input city $15.;

Note that any solution you use wlll leave CITY with a length of $15.  So any leading blanks that are removed will become trailing blanks instead, to reach that length of $15,.

 

It's not clear if you want to change multiple embedded blanks.  For example, do you want to end up with 

"San   Diego"  vs. "San Diego"

If that is the issue, apply the COMPBL function. 

Amir
PROC Star

Hi @BrahmanandaRao,

 

As you have done with the input data, please supply another data step with datalines that show what output data you want based on the input data you have provided.

 

Based on the responses you've had so far, showing your desired output should help clarify your requirements.

 

To "remove leading blanks" (as per your subject title), you can look at the left() function definition and example in the documentation  to see if it should give you what you want.

 

 

Kind regards,

Amir.

s_lassen
Meteorite | Level 14

I assume that you both want to get rid of the leading blanks and reduce the multiple blanks inside the names to single blanks.

 

For the first thing, use LEFT. For the next thing, use COMPBL, e.g.:

city=left(compbl(city));

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 7759 views
  • 5 likes
  • 6 in conversation