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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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