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));

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5079 views
  • 5 likes
  • 6 in conversation