Hi there, I am working in SAS Studio for SAS On Demand version 9.4_M6 operating on Linux X64. I am having trouble nesting two functions, one to concatenate 3 already existing variables and the other to remove periods in the observations. Here is the question from my homework, and I have attached the dataset that is referenced in the question. "The data set contains the following 3 variables: FirstInit, MiddleInit, and LastInit. Write an expression which: 1) Uses one of the concatenation functions to contatenate the 3 variables above into a single variable named ‘Inits’. 2) Nest this within a function which will remove the periods from the value. Note: If performed correctly, the first observation will have a value of ‘SYB’." I tried this code below and it ended up giving me an extra period at the end of each observation instead of removing them. Please let me know if anyone knows how to fix this! Thank you! Sincerely, Kaitlin E Buck
Take a quick look at this code:
DATA VIP_Data;
DROP _:;
_FirstInit = 'K.';
_MiddleInit = 'E.';
_LastInit = 'B.';
FirstTry = compress(cat(_FirstInit,_MiddleInit,_LastInit,'.'));
SecondTry = compress(cat(_FirstInit,_MiddleInit,_LastInit),'.');
RUN;
Now, here's my results dataset:
FirstTry SecondTry K.E.B.. KEB
Do you see how the first try not only doesn't have the existing periods removed but has an extra trailing period?
In this command:
cat(_FirstInit,_MiddleInit,_LastInit,'.')
You instructed SAS to tack on a period.
You then took that result and ran it through a COMPRESS. Compress without any qualifiers removes spaces.
The trick to debugging nested functions is to "decompose" them, that is to un-nest them and try them one step at time. Like so (second Data step):
DATA VIP_Data;
DROP _:;
_FirstInit = 'K.';
_MiddleInit = 'E.';
_LastInit = 'B.';
FirstTry = compress(cat(_FirstInit,_MiddleInit,_LastInit,'.'));
SecondTry = compress(cat(_FirstInit,_MiddleInit,_LastInit),'.');
RUN;
DATA VIP_Data2;
DROP _:;
_FirstInit = 'K.';
_MiddleInit = 'E.';
_LastInit = 'B.';
Temp_Val = cat(_FirstInit,_MiddleInit,_LastInit,'.');
Final_Val = compress(Temp_Val);
RUN;
Once you get used to nesting functions, go for it, but when you need to debug them, break them apart and check the intermediate values.
Jim
@kateb409, you have mis-placed the period. The period needs to be after the close parenthesis for CAT. Right now, the period is inside the parenthesis, giving you an extra period.
Jim
Hi Jim,
So my statement should look like this?:
Inits = compress(cat(FirstInit,MiddleInit,LastInit,)'.');
-Kaitlin
Close. 🙂 The last comma in the CAT should be moved to after the parenthesis.
Jim
Take a quick look at this code:
DATA VIP_Data;
DROP _:;
_FirstInit = 'K.';
_MiddleInit = 'E.';
_LastInit = 'B.';
FirstTry = compress(cat(_FirstInit,_MiddleInit,_LastInit,'.'));
SecondTry = compress(cat(_FirstInit,_MiddleInit,_LastInit),'.');
RUN;
Now, here's my results dataset:
FirstTry SecondTry K.E.B.. KEB
Do you see how the first try not only doesn't have the existing periods removed but has an extra trailing period?
In this command:
cat(_FirstInit,_MiddleInit,_LastInit,'.')
You instructed SAS to tack on a period.
You then took that result and ran it through a COMPRESS. Compress without any qualifiers removes spaces.
The trick to debugging nested functions is to "decompose" them, that is to un-nest them and try them one step at time. Like so (second Data step):
DATA VIP_Data;
DROP _:;
_FirstInit = 'K.';
_MiddleInit = 'E.';
_LastInit = 'B.';
FirstTry = compress(cat(_FirstInit,_MiddleInit,_LastInit,'.'));
SecondTry = compress(cat(_FirstInit,_MiddleInit,_LastInit),'.');
RUN;
DATA VIP_Data2;
DROP _:;
_FirstInit = 'K.';
_MiddleInit = 'E.';
_LastInit = 'B.';
Temp_Val = cat(_FirstInit,_MiddleInit,_LastInit,'.');
Final_Val = compress(Temp_Val);
RUN;
Once you get used to nesting functions, go for it, but when you need to debug them, break them apart and check the intermediate values.
Jim
Hi @jimbarbour ,
This worked, thank you so much! I didn't realize a comma needed to be there. I also decided to use the cats function instead of the standard cat function as well to remove the additional spaces in between the initials.
Inits = compress(cats(FirstInit,MiddleInit,LastInit),'.');
Sincerely,
Kaitlin E Buck
Hi, @kateb409,
Oh, good. I'm glad it was helpful.
If it were of interest, this paper lists out the various CAT functions (CAT, CATT, CATS, CATX, and CATQ):
https://www.lexjansen.com/wuss/2016/109_Final_Paper_PDF.pdf
If you're ever in need of papers on SAS (which I find extremely helpful), lexjansen.com is one of the first places I would go.
Jim
When figuring out how to nest functions build them separately at first and then copy and replace the parts once it's working.
part1 = cat(firstinit, middleinit, lastinit);
part2 = compress(part1, '.');
combined = compress(cat(firstinit, middleinit, lastinit), '.')
HTH
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.