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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.