Hello,
I have variable values like below:
RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE
RIGHT HAND; LEFT LEG
HEART
I would like to create a new variable like below:
HEART
LEFT LEG
LEFT LYMPH NODE
RIGHT EXTERNAL LYMPH
RIGHT HAND
RIGHT LYMPH NODE
Which means:
1. separate values between special characters
2. Create a new variable in alphabetical order with those separated values.
Can you please help me.
Thanks,
Nyalamadugu
Use the SCAN() function to parse the strings at the semi-colons. Output each value as a separate observation. Then sort.
data have;
row+1;
input string $80.;
cards4;
RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE
RIGHT HAND; LEFT LEG
HEART
;;;;
data want;
set have;
do index=1 to countw(string,';');
word=strip(scan(string,index,';'));
output;
end;
run;
proc sort;
by word;
run;
Results
Obs row string index word 1 3 HEART 1 HEART 2 2 RIGHT HAND; LEFT LEG 2 LEFT LEG 3 1 RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE 3 LEFT LYMPH NODE 4 1 RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE 2 RIGHT EXTERNAL LYMPH 5 2 RIGHT HAND; LEFT LEG 1 RIGHT HAND 6 1 RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE 1 RIGHT LYMPH NODE
Use the SCAN() function to parse the strings at the semi-colons. Output each value as a separate observation. Then sort.
data have;
row+1;
input string $80.;
cards4;
RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE
RIGHT HAND; LEFT LEG
HEART
;;;;
data want;
set have;
do index=1 to countw(string,';');
word=strip(scan(string,index,';'));
output;
end;
run;
proc sort;
by word;
run;
Results
Obs row string index word 1 3 HEART 1 HEART 2 2 RIGHT HAND; LEFT LEG 2 LEFT LEG 3 1 RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE 3 LEFT LYMPH NODE 4 1 RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE 2 RIGHT EXTERNAL LYMPH 5 2 RIGHT HAND; LEFT LEG 1 RIGHT HAND 6 1 RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE 1 RIGHT LYMPH NODE
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.