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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

3 REPLIES 3
Haikuo
Onyx | Level 15

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;

nbonda
Obsidian | Level 7

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

Haikuo
Onyx | Level 15

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 1276 views
  • 4 likes
  • 2 in conversation