I'm new in sas, I need help how to move specific word to new column in sas, with the data below :
address :
TEGALASEM RT 005 RW 003 KEL SUMBERGONDO KEC BUMIAJI
CEMARA BARU III F 512 RT 009 RW 017 KEL JATIMULYA KEC TAMBUN SELATAN
JL LIGAR TIMUR 1 NO 1 RT 01/13 KEL CIBEUNYING
result :
KEL : KEC :
SUMBERGONDO BUMIAJI
JATIMULYA TAMBUN SELATAN
CIBEUNYING
data have;
input have $80.;
cards;
TEGALASEM RT 005 RW 003 KEL SUMBERGONDO KEC BUMIAJI
CEMARA BARU III F 512 RT 009 RW 017 KEL JATIMULYA KEC TAMBUN SELATAN
JL LIGAR TIMUR 1 NO 1 RT 01/13 KEL CIBEUNYING
;
data want;
set have;
pid=prxparse('/(KEL\s+\w+\s+)(KEC[\w\s]+)?/i');
if prxmatch(pid,have) then do;
kel=left(substr(prxposn(pid,1,have),4));
kec=left(substr(prxposn(pid,2,have),4));
end;
drop pid;
run;
What is the logic here?
if "KEL" in the address then move to the KEL column
if the "KEC" in the address then move to the KEL column
Data :
| no | address |
| 1 | TEGALASEM RT 005 RW 003 KEL SUMBERGONDO KEC BUMIAJI |
| 2 | CEMARA BARU III F 512 RT 009 RW 017 KEL JATIMULYA KEC TAMBUN SELATAN |
| 3 | JL LIGAR TIMUR 1 NO 1 RT 01/13 KEL CIBEUNYING |
Result
| NO | KEL | KEC |
| 1 | SUMBERGONDO | BUMIAJI |
| 2 | JATIMULYA | TAMBUN SELATAN |
| 3 | CIBEUNYING |
Use the FINDW Function with the "e" modifier to see if the KEL or KEC is present, then extract the following word with SCAN.
data have;
input have $80.;
cards;
TEGALASEM RT 005 RW 003 KEL SUMBERGONDO KEC BUMIAJI
CEMARA BARU III F 512 RT 009 RW 017 KEL JATIMULYA KEC TAMBUN SELATAN
JL LIGAR TIMUR 1 NO 1 RT 01/13 KEL CIBEUNYING
;
data want;
set have;
pid=prxparse('/(KEL\s+\w+\s+)(KEC[\w\s]+)?/i');
if prxmatch(pid,have) then do;
kel=left(substr(prxposn(pid,1,have),4));
kec=left(substr(prxposn(pid,2,have),4));
end;
drop pid;
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!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.