Tuesday, October 21, 2008

A code to split numbers!

-
The dummy dataset code has numbers separated by a hash or space. The code that follows splits those numbers and output each number into a unique observation.
-
***** DUMMY DATASET WITH #, " " *****
-
data TEST;
length pt $20 txt $40;
pt ='1';
txt= '#12, #3, #56 34-98';
output;
pt ='2';
txt= '#2 #12';
output;
pt ='3';
txt= '#12';
output;
run;
-
***** SPLIT & OUTPUT NUMBERS *****
-
data TEST1;
set TEST;
stat:
do i = 1 to length(txt);
-
if substr(txt,i,1) = '#' then do;
-
if substr(txt,i+2,1) in (',','') then do;
cno = substr(txt,i+1,1); *3: for single digit;
txt = substr(txt,i+2);
end;
else if substr(txt,i+3,1) in (',','') then do;
cno = substr(txt,i+1,2); *12: for two digit;
txt = substr(txt,i+3);
end;
-
output;
if length(trim(left(txt))) > 2 then goto stat;
leave;
end;
-
end;
run;
-
***** SAME CODE FOR DATA WITHOUT # *****
-
data TEST1;
set TEST;
do i = 1 to length(txt);
-
if substr(txt,i+1,1) in (',','')
then do;
cno = trim(left(substr(txt,i,1)));
i=i+2;
end;
else if substr(txt,i+2,1) in (',','')
then do;
cno = trim(left(substr(txt,i,2)));
i=i+3;
end;
-
if
cno ne ''
then output;
end;
run;
-

No comments:

Post a Comment