DATA Step, Macro, Functions and more

How to fill the blanks within string with underscore

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

How to fill the blanks within string with underscore

how to fill blanks with underscore, I have city_name variable that has multiple words, so of them have multiple blanks and dashes (-).

I tried this way:

data city;

set city;

city_name=trim(city_name);

city_name=compres(city_name, '-');

city_name=tranwrd(city_name, ' ','_');

run;

But I am getting underscores at the end of city name.  How to solve this problem?


Accepted Solutions
Solution
‎05-14-2015 05:17 PM
Respected Advisor
Posts: 3,156

Re: How to fill the blanks within string with underscore

you will need to remove the trailing blanks on the fly:

data city;

set city;

/*city_name=trim(city_name);*/

city_name=compress(city_name, '-');

city_name=tranwrd(trim(city_name), ' ','_');

run;

View solution in original post


All Replies
Solution
‎05-14-2015 05:17 PM
Respected Advisor
Posts: 3,156

Re: How to fill the blanks within string with underscore

you will need to remove the trailing blanks on the fly:

data city;

set city;

/*city_name=trim(city_name);*/

city_name=compress(city_name, '-');

city_name=tranwrd(trim(city_name), ' ','_');

run;

Contributor
Posts: 40

Re: How to fill the blanks within string with underscore

Thank you...what's the difference in using trim function separately and on the fly

Respected Advisor
Posts: 3,156

Re: How to fill the blanks within string with underscore

It is not about trim function or any function, SAS data step uses FIXED length variables, so if not 'on the fly', even the blanks have been removed by Trim function (in this case), Blanks will be padded back in all the way to the length of your variable next time when you call it.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 237 views
  • 4 likes
  • 2 in conversation