Loop through the string, incrementing the loop counter by 2:
data have;
input name $80.;
cards;
Aaron Smith Andrew Knoi
Linda Jackson Betty Young
;
data want;
set have;
i = 1;
word = scan(name,i);
do while(word ne "");
n_name = catx(" ",scan(name,i),scan(name,i+1));
output;
i = i + 2;
word = scan(name,i);
end;
keep n_name;
rename n_name=name;
run;
Loop through the string, incrementing the loop counter by 2:
data have;
input name $80.;
cards;
Aaron Smith Andrew Knoi
Linda Jackson Betty Young
;
data want;
set have;
i = 1;
word = scan(name,i);
do while(word ne "");
n_name = catx(" ",scan(name,i),scan(name,i+1));
output;
i = i + 2;
word = scan(name,i);
end;
keep n_name;
rename n_name=name;
run;
It's actually easier if you create two variables instead of one:
data have;
length first_name last_name $ 20;
input first_name last_name @@;
cards;
Aaron Smith Andrew Knoi
Linda Jackson Betty Young
;
Can you add an example showing what should happen if the value has to separated at the third space?
data have;
input name $40.;
cards;
Aaron Smith Andrew Knoi
Linda Jackson Betty Young
;
run;
data want;
set have;
call scan(name,2,p,l,' ');
new_name=left(substr(name,1,p+l));output;
new_name=left(substr(name,p+l));output;
drop p l;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.