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

Hello

 

I am trying to add a period for a name column only when the name shows up as one letter.

data have;

middle_name;

datalines

M

Tim

Bob

I

Tina

 

I tried to do this:

data want;

set have;

if length(strip(middle_name)) = 1 then

middle_name = catx('.', middle_name, 1, 1), substr(middle_nm,1));

run;

 

but the middle initials show as M.M and I.I

 

Any help would be greatly appreciated! Definitely am a beginner. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

if length(strip(middle_name)) = 1 and not missing(middle_name) then

middle_name = cats( middle_name,'.');

View solution in original post

4 REPLIES 4
ballardw
Super User

Try

if length(strip(middle_name)) = 1 then

middle_name = cats( middle_name,'.');

 

CATX only places the character BETWEEN things. The CATS will append the period (or other string) and you have already checked that you only want to do that when there is only one character

sasperson1
Calcite | Level 5
Oh thank you! That worked for the most part. I forgot to add that some values will be blank and when I ran this, it added the period to blank values. Is there a way to avoid that? Does that make sense?

M

Tim
Bob

I
Tina

Thanks for your help!
ballardw
Super User

if length(strip(middle_name)) = 1 and not missing(middle_name) then

middle_name = cats( middle_name,'.');

sasperson1
Calcite | Level 5

Thank you so much! That worked.

 

Cheers!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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