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;
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 save with the early bird rate—just $795!
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.