SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
data _null_;
	separator='*';
	x='SAS';
	y=catx(separator,x);
	put y=;
	run;
	

Desired output;

S*A*S

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Run a loop over the string that selects single characters with SUBSTR, and concatenate those:

do i = 1 to length(x);
  y = catx(separator,substr(x,i,1));
end;

Make sure that the defined length of y is sufficient (at least double the defined length of x minus 1).

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Run a loop over the string that selects single characters with SUBSTR, and concatenate those:

do i = 1 to length(x);
  y = catx(separator,substr(x,i,1));
end;

Make sure that the defined length of y is sufficient (at least double the defined length of x minus 1).

BrahmanandaRao
Lapis Lazuli | Level 10
data n;
do i = 1 to length(x);
separator='*';
y = catx(separator,substr(x,i,1));
end ;
run;
BrahmanandaRao
Lapis Lazuli | Level 10
i didn't get desired ouput above code
Kurt_Bremser
Super User

Please supply example data in a data step with datalines (DO NOT SKIP THIS!), and the code you ran against that dataset, and indicate where the result does not meet your expectations.

BrahmanandaRao
Lapis Lazuli | Level 10
data s;
x='SAS';
do i=1 to length(x);
y=catx('*',substr(x,i,1));
end;
run;
Kurt_Bremser
Super User

My bad, y was missing as argument to the CATX function:

data s;
x = 'SAS';
length y $5;
do i = 1 to length(x);
  y = catx('*',y,substr(x,i,1));
end;
drop i;
run;
BrahmanandaRao
Lapis Lazuli | Level 10
Perfect
Thank You

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2390 views
  • 0 likes
  • 2 in conversation