BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
smackerz1988
Pyrite | Level 9

Hello,

 

I basically have two values within a column that I need to add a space after a hyphen. Would a combination of find or regex expressions work here?.

 

data have;
input  SUBJID $ INVNAM :$30.; 
infile datalines dlm = '|';
datalines;
1001|Raman
1002|Rodgers
1003|Ekanayake-Bohlig
1004|Renczynska-Matysko
;
run;


data want;
input  SUBJID $ INVNAM :$30.; 
infile datalines dlm = '|';
datalines;
1001|Raman
1002|Rodgers
1003|Ekanayake- Bohlig
1004|Renczynska- Matysko
;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mtnbikerjoshua
Obsidian | Level 7

Hi @smackerz1988 ,

 

You can replace the hyphen with a hyphen followed by a space, like this:

tranwrd(invnam, "-", "- ")

A regex expression is unnecessary in this case, but the code would look like this:

prxchange('s/-/- /', 1, invnam)

If you had more complex requirements, such as add a space only if the hyphen is after the first word or only if the hyphen is not at the end of the string, a regex expression would be more appropriate.

 

I hope that clarifies what @data_null__ meant.

 

Joshua

 

View solution in original post

5 REPLIES 5
data_null__
Jade | Level 19

Required Arguments

source

specifies a character constant, variable, or expression that you want to translate.

target

specifies a character constant, variable, or expression that is searched for in source.

Requirement The length for target must be greater than zero.

replacement

specifies a character constant, variable, or expression that replaces target. When the replacement string has a length of zero, TRANWRD uses a single blank instead.

smackerz1988
Pyrite | Level 9

But I want to add a space not replace the hyphen with a space

mtnbikerjoshua
Obsidian | Level 7

Hi @smackerz1988 ,

 

You can replace the hyphen with a hyphen followed by a space, like this:

tranwrd(invnam, "-", "- ")

A regex expression is unnecessary in this case, but the code would look like this:

prxchange('s/-/- /', 1, invnam)

If you had more complex requirements, such as add a space only if the hyphen is after the first word or only if the hyphen is not at the end of the string, a regex expression would be more appropriate.

 

I hope that clarifies what @data_null__ meant.

 

Joshua

 

mtnbikerjoshua
Obsidian | Level 7
You're welcome! Also, by the way, thank you for posting example data.
That always makes it much easier to answer a question.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 1954 views
  • 3 likes
  • 3 in conversation