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;
any ideas?
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;
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.