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

Hi all,

 

I'm trying to figure out how to replace the spaces within the Init variable with dashes ("-"). I've already compressed the variable to remove the periods from the raw data (3rd pic below), but just need the final initial format to be in FML or F-L if missing data. Thanks!

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The TRANSLATE() function is for replacing characters with other characters. Watch out for two things. Remember to trim the trailing blanks from your variable's value.  And the arguments for TRANSLATE() are in the opposite order of what you normally expect.

data want;
  set have;
  myvar = translate(trim(myvar),'-',' ');
run;

PS Please post text as text, not as photographs.

View solution in original post

3 REPLIES 3
Reeza
Super User
You know there's an SSN format?
If your variable is numeric you can apply the format directly to get the desired displayed value.
https://documentation.sas.com/?cdcId=vdmmlcdc&cdcVersion=1.0&docsetId=leforinforref&docsetTarget=n1s...

The process for INITS is similar to what you've done with SSN, but you can use COUNTC() or LENGHT to count the number of periods/letters to deal with the two versus three letter cases.


inits_new = compress(inits);
if length(inits_new) = 2 then inits_new = catx('-', char(inits_new, 1), char(inits_new, 2));

dthompsonada
Obsidian | Level 7
Rohana-
Can you copy and paste the text of the code in a reply? This isn't legible when I zoom.
Thanks!
Tom
Super User Tom
Super User

The TRANSLATE() function is for replacing characters with other characters. Watch out for two things. Remember to trim the trailing blanks from your variable's value.  And the arguments for TRANSLATE() are in the opposite order of what you normally expect.

data want;
  set have;
  myvar = translate(trim(myvar),'-',' ');
run;

PS Please post text as text, not as photographs.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 7930 views
  • 3 likes
  • 4 in conversation