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

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

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

@kateb409,

 

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

 

 

View solution in original post

7 REPLIES 7
jimbarbour
Meteorite | Level 14

@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

kateb409
Obsidian | Level 7

Hi Jim, 

So my statement should look like this?:

Inits = compress(cat(FirstInit,MiddleInit,LastInit,)'.'); 

-Kaitlin

jimbarbour
Meteorite | Level 14

Close.  🙂  The last comma in the CAT should be moved to after the parenthesis.

 

Jim

jimbarbour
Meteorite | Level 14

@kateb409,

 

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
Obsidian | Level 7

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

jimbarbour
Meteorite | Level 14

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

 

Reeza
Super User

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 7 replies
  • 1757 views
  • 4 likes
  • 3 in conversation