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

I try to concatenate a text-string with a do loop. the functions catt, cats and catx works, but cat doesn't. but i need the blanks in the ouptut string.

 

 

data want;

	length string_in string_out_cat string_out_cats string_out_catt string_out_catx $200 c $1;

	string_in = "this is a textstring";
	do i = 1 to lengthc(string_in);
		c = substr(string_in,i,1);
		string_out_cat = cat(string_out_cat,c);
		string_out_cats = cats(string_out_cats,c);
		string_out_catt = catt(string_out_catt,c);
		string_out_catx = catx("*",string_out_catx,c);
	end;

	

run;


concat.PNG

 

any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
optimus
Fluorite | Level 6

Thanks for your answer. This was the hint I needed. Now it make sence.

 

I want to keep the space between the word, but I have to loop / process each letter. So the final solution is this:

 

data want (drop=c i ablank);

	length string_in string_out $50 c $1;

	string_in = "this is a textstring";
	ablank=0;

	do i = 1 to lengthc(string_in);
		c = substr(string_in,i,1);
		
		if ablank=1 then string_out = cat(strip(string_out)," ",c);
		else string_out = cat(strip(string_out),c);
		
		if c=" " then ablank=1;
		else ablank=0;
			
	end;

run;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is due to the way strings are handled.  When you set a new variable of a given length, say 5 in this case then the variable is:

[][][][][]

Now if you choose the procedure that keeps blanks and concatenates variables and add ABCDE:

[][][][][][a][b][c][d][e]

Then put that back into the chracter that has length 5 it chops the end off, hence the variable can only contain 5 spaces.

Thus the question back to you is what do you expect the output to look like?  is it a space between words, or letters?  If its words then you would be better off doing:

data want;
  ...
  do i=1 to countw(string," ");
    word=scan(string,i," ");
  end;
run;

If its each letter, then catx() would be the function.  But without further info I can't say.

optimus
Fluorite | Level 6

Thanks for your answer. This was the hint I needed. Now it make sence.

 

I want to keep the space between the word, but I have to loop / process each letter. So the final solution is this:

 

data want (drop=c i ablank);

	length string_in string_out $50 c $1;

	string_in = "this is a textstring";
	ablank=0;

	do i = 1 to lengthc(string_in);
		c = substr(string_in,i,1);
		
		if ablank=1 then string_out = cat(strip(string_out)," ",c);
		else string_out = cat(strip(string_out),c);
		
		if c=" " then ablank=1;
		else ablank=0;
			
	end;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 2237 views
  • 1 like
  • 2 in conversation