Hello SAS Communities,
I am looking to change an Initials variable into the form of FML and if there is no middle initial F-L.
The current initials variable is in the form F.M.L and when there is no middle initial it looks like this F. L.
The output I have
The output I want is
Inits
RWP
NAE
TCJ
FLY
GSC
S-R
etc
Here is what I have so far
DATA WORK.data ;
SET import.records (RENAME=(Inits = initials ID=social ZipCode=ZipCd));
Inits
inits2 = COMPRESS(initials, '.');
IF length(inits2) = 2 THEN Inits = CATX('-', CHAR(inits2, 1), CHAR(inits2, 2));
ELSE Inits= inits2;
RUN;
This does not work because those with missing middle initials get outputted as just their first initial, ie S. R. is just S.
Thank you!
When you apply the COMPRESS function you remove the dots only. You need to remove blanks as well in order to get a length of 2. So the second parameter to COMPRESS should be:
inits2 = COMPRESS(initials, ' .');
That will get S. R. to come out as SR. WIthout that change, you are getting S R.
Your test looks good. What do you dislike?
When you apply the COMPRESS function you remove the dots only. You need to remove blanks as well in order to get a length of 2. So the second parameter to COMPRESS should be:
inits2 = COMPRESS(initials, ' .');
That will get S. R. to come out as SR. WIthout that change, you are getting S R.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.