BookmarkSubscribeRSS Feed
cass5957
Calcite | Level 5

Hi all! I'm having some issues getting the "inits" variable to be 3 characters and keep all 3 initials. So far, the inits variable is 24 characters and if I add a length statement "Inits $3;" it only outputs the first initial (not FML)

This is my code: 

 

DATA WORK.State;
SET WORK.State2 (RENAME = (SocSecNum = SSN));
;
FirstInit=COMPRESS(FirsitInit, '.');
xMiddleInit=COMPRESS(MiddleInit, '.');
xLastInit=COMPRESS(LastInit, '.');
Inits = FirsitInit || MiddleInit || LastInit;
Initials = TRANWRD(COMPRESS(Inits, '. '),' ' , '-' );
Inits = Initials;

RUN;

 

Thank you!

2 REPLIES 2
Tom
Super User Tom
Super User

If you define INITS as length of only 3 and try to assign it a value that is longer than that it will just keep the first three characters.

In this assignment statement

Inits = FirstInit || MiddleInit || LastInit;

will only work if each of those other variables are defined with a length of one. For example if FIRSTINIT has a length of 8 then only the first three characters from that variable will fit in the new INITS variable and whatever values the other two variables had wouldn't matter.

Did you mean?

Inits = char(FirstInit,1)||char(MiddleInit,1)||char(LastInit,1);

 

 

txim33
Calcite | Level 5

Hi cass5957! 

That was a tricky problem! 

Here is the code that I used:

 

Inits = CAT(SUBSTR(FirstInit,1,1), SUBSTR(MiddleInit,1,1), SUBSTR(LastInit,1,1));

 

Otherwise if you want to use the compress function I also was able to get this code to work in the same way:

Inits = COMPRESS(CAT(FirstInit, MiddleInit, LastInit), '.');

 

Hope this helps! 

A